ShaggyPE
Mechanical
- Sep 8, 2003
- 1,127
First off, I want to thank everyone who helped me solve my earlier macro problem. Now, I have a new one. This macro will be part of a larger macro once I get the functionality down.
The goal here is to run the macro in solidworks, it will open a specific excel spreadsheet located in the same directory as the macro file. Within that spreadsheet is some specific text. The macro will find that text and return the value of the cell two to the right from the cell where it found
the original value.
I initially created a macro in Excel by recording it while I did the find operation. I then modified the code to do the offset operation. That macro runs beautifully within excel.
When I try to incorporate that code into the SW macro, all things go awry. I believe I have used the GetObject function properly, but it seems like the macro is getting stuck in some of the excel functionality. See the bolded code. When I run the macro I get an error stating: Run-time error '438': Object doesn't support this property or method.
This leads me to believe that either I can't perform some api operations outside of excel that I can perform within (doubtful), or I am not completely in excel yet (maybe), or I suck (likely). The errored code is in bold below.
If anybody has any sample code that grabs data from excel, I would love to see it. I did some searching and the only mention I found was TheTicks xyz points macro, but that doesn't seem available for download anywhere (I checked his link). The macro I have started is based on Lenny Kikstra's SourceFileAccess macro, but that is intended to grab from a text file. I also sampled from his AssemblyBOM macro, but that one creates an excel file using the createobject function, I believe I need to use the getobject function.
Any help is appreciated.
-Shaggy
The goal here is to run the macro in solidworks, it will open a specific excel spreadsheet located in the same directory as the macro file. Within that spreadsheet is some specific text. The macro will find that text and return the value of the cell two to the right from the cell where it found
the original value.
I initially created a macro in Excel by recording it while I did the find operation. I then modified the code to do the offset operation. That macro runs beautifully within excel.
When I try to incorporate that code into the SW macro, all things go awry. I believe I have used the GetObject function properly, but it seems like the macro is getting stuck in some of the excel functionality. See the bolded code. When I run the macro I get an error stating: Run-time error '438': Object doesn't support this property or method.
This leads me to believe that either I can't perform some api operations outside of excel that I can perform within (doubtful), or I am not completely in excel yet (maybe), or I suck (likely). The errored code is in bold below.
Code:
Option Explicit
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long
Dim FileSys As Object
Dim Source As String
Dim AttValue As String
Private Sub ReadProperties()
Dim XLApp As Object
Set XLApp = GetObject(Source)
'XLApp.Visible = True
XLApp.Worksheets("sheet1").Activate
[b]XLApp.Columns("B:B").Select[/b]
XLApp.Selection.Find(What:="1001001", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False).Activate
XLApp.ActiveCell.Offset(rowOffset:=0, columnOffset:=2).Activate
AttValue = XLApp.ActiveCell.Value
End Sub
Private Sub SetDefaults()
MsgBox "Source file not found. Using macro default."
'
' Add routines here for setting defaults.
'
End Sub
Sub Main()
Set swApp = CreateObject("SldWorks.Application")
Source = swApp.GetCurrentMacroPathName ' Get macro Path FileName
Source = Left$(Source, Len(Source) - 26) + "SOLIDWORKS.xls" ' Set Source FileName
MsgBox (Source)
Set FileSys = CreateObject("Scripting.FileSystemObject")
If FileSys.FileExists(Source) Then ' Does source file exist?
ReadProperties ' Yes, read file
Else
SetDefaults ' No, use macro defaults
End If
'
'
MsgBox (AttValue)
End Sub
If anybody has any sample code that grabs data from excel, I would love to see it. I did some searching and the only mention I found was TheTicks xyz points macro, but that doesn't seem available for download anywhere (I checked his link). The macro I have started is based on Lenny Kikstra's SourceFileAccess macro, but that is intended to grab from a text file. I also sampled from his AssemblyBOM macro, but that one creates an excel file using the createobject function, I believe I need to use the getobject function.
Any help is appreciated.
-Shaggy