I've got a problem with a macro we use to save drawings as DWGs and PDFs in SolidWorks 2010 SP5.0. This macro used to work great (I know we *had* it working in SW2010 at one point), but now when I try to run it, I get a "Run-time error '91': Object variable or With block variable not set".
Here's the text of the macro. The error is occuring at the "Set model = swApp.ActiveDoc" line.
The references I have enabled are:
Visual Basic for Applications
SolidWorks Extensibility Type Library
OLE Automation
SldWorks 2010 Type Library
SolidWorks 2010 Constant type library
SolidWorks Utilities 2010 Type library
Am I missing a library I need to make this work? Or did something change in the SolidWorks 2010 libraries that broke it maybe?
' ******************************************************************************
' SaveAsDWG-PDFOrder_2010 macro 7/7/11
' ******************************************************************************
' This Macro is used to create .dwg and .pdf versions of SolidWorks drawings in the order directory on the Intranet.
'Declare variables
Dim swApp As SldWorks.SldWorks
Dim model As SldWorks.ModelDoc2
Dim Name As String
Dim PartRev As String
Dim SavePath As String
Dim SavePathWork As String
Dim Filename As String
Dim Order As String
Sub main()
Set swApp = Application.SldWorks
Set model = swApp.ActiveDoc
'check to see if SWdocument is open
If model Is Nothing Then
MsgBox ("Please open a SolidWorks Drawing")
End
End If
'Retrieve filename including path
Name = model.GetPathName()
'Prompt to enter order number
Order = InputBox("Please enter the order directory name.")
'Defines Intranet Order File Path (Edit Macro if FilePath must be changed)
SavePath = "\\Sar-3\confpage\Drawings\" & Order & "\"
'error trap
If Name = "" Then
MsgBox ("Please save the file first.")
model.Save
End
End If
'Strip off filepath to get just filename, then
Filename = Mid\$(Name, InStrRev(Name, "\") + 1)
'Remove .slddrw extension
Name = Left\$(Filename, Len(Filename) - 7)
'Save As DWG and PDF file
model.SaveAs4 SavePath & Name & ".DWG", 0, 0, 0, 0
model.SaveAs4 SavePath & Name & ".PDF", 0, 0, 0, 0
'Inform user of save status
MsgBox ("The DWG and PDF files have been saved to the Intranet directory for the order.")
End Sub
SolidworksApi macros