calebfulks
New Member
- Joined
- Dec 9, 2011
- Messages
- 3
I have enough experience with VBA and macros to get into trouble, and this is my first post here.
I have written a macro to be usde from Solidworks 2011 that opens an excel file, finds a part number(ModName) and then updates some values that are associated with that part number. After getting that portion to work, I added an If then statement using the activeworkbook.readonly to ensure that the user has aces to the excel file. If I step through the program with F8 inside visual basic, the program works as planned every time, but when I run the macro in Solidworks it gives a an error every-other time: run-time error '91', Object Variable or With block not set. Any help would be very appreciated.
There is a second set of code below what I have shown here, hence all of the extra declarations.
I have written a macro to be usde from Solidworks 2011 that opens an excel file, finds a part number(ModName) and then updates some values that are associated with that part number. After getting that portion to work, I added an If then statement using the activeworkbook.readonly to ensure that the user has aces to the excel file. If I step through the program with F8 inside visual basic, the program works as planned every time, but when I run the macro in Solidworks it gives a an error every-other time: run-time error '91', Object Variable or With block not set. Any help would be very appreciated.
There is a second set of code below what I have shown here, hence all of the extra declarations.
Code:
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Revision As String
Dim Proptype As String
Dim Partnumber As String
Dim MyPath, ModName, NewName1, NewName2, OldRevPath As String
Dim MB1 As Boolean
Dim MB2 As Boolean
Dim Errs As Long
Dim Warnings As Long
Dim swFrame As SldWorks.Frame
Dim FSO As Object
Dim OldRev As Object
Sub main()
Set swApp = Application.SldWorks
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set Model = swApp.ActiveDoc
Set swFrame = swApp.Frame
Revision = Part.GetCustomInfoValue("", "Revision")
ModName = Left(Model.GetTitle, InStrRev(Model.GetTitle, " Sheet") - 3)
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
If GetAttr("X:\Book1.xlsx") And vbReadOnly Then
MsgBox "You Do NOT have write acces to the Number Log"
Exit Sub
End If
objExcel.Workbooks.open ("x:\Book1.xlsx")
If activeworkbook.ReadOnly Then
MsgBox "You Do NOT have write acces to the Number Log"
objExcel.activeworkbook.Close
Exit Sub
End If
Cells.Find(What:=ModName, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Select
ActiveCell.Offset(0, 2).Select
objExcel.ActiveCell.Value = Revision
ActiveCell.Offset(0, -3).Select
objExcel.ActiveCell.Value = "Released"
objExcel.activeworkbook.Save
objExcel.activeworkbook.Close