Hi guys, I'm writing a macro to automate some tasks...as all do...and keep having trouble with this part that is designed to (from the model) save out a step file of the part/assembly into a location on my desktop, open the drawing of the part/assembly, then save all pages of the drawing as a pdf in the same location on the desktop. Some reason I can never seem to get a PDF to generate right... but here it goes. The issue I'm having is at the first boolean command I'm getting a "Object variable or With block variable not set" error. Am I using the boolean command wrong? Is there something better I should be doing?
Option Explicit
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swExportData As SldWorks.ExportPdfData
Dim boolstatus As Boolean
Dim lErrors As Long
Dim lWarnings As Long
Dim Part2 As Object
Dim longstatus As Long, longwarnings As Long
Dim dwgPath As String
Dim dwgfPath As String
Dim Model As ModelDoc2
Dim ModelDraw As Object
Dim swModelName As String
Dim NewPath As String
Dim PDFPath As String
Dim swApp As Object
Sub Main()
'Generate Attachments
Set swApp = Application.SldWorks
Set Model = swApp.ActiveDoc
swModelName = Model.GetTitle
NewPath = "C:\\Users\\2492663\\Desktop\\Outsource Files\\" & swModelName & ".STEP"
Debug.Print "Model name is " & swModelName; " - swModelName" & vbCrLf
Debug.Print "Original model location is " & Model.GetPathName & vbCrLf
'Model.SaveAs (NewPath)
Debug.Print "New Save Location is " & NewPath; " - NewPath "
dwgPath = Model.GetPathName ' Get the path of the current part
dwgfPath = (Strings.Left(dwgPath, Strings.Len(dwgPath) - 6)) & "SLDDRW" ' Find the drawing associated with that part
' Open the drawing
Set Part2 = swApp.OpenDoc6(dwgfPath, 3, 0, "", longstatus, longwarnings)
If (Part2 Is Nothing) Then
' do nothing
Else
'Save Drawing as PDF
Set Part2 = swApp.ActiveDoc
PDFPath = "C:\\Users\\2492663\\Desktop\\Outsource Files\\" & swModelName & ".PDF"
boolstatus = swExportData.SetSheets(swExportData_ExportAllSheets, 1)
boolstatus = swModelDocExt.SaveAs(PDFPath, 0, 0, swExportData, lErrors, lWarnings)
End If
End Sub
SolidworksApi/macros
You declared swModelDocExt and swExportData but you never actually set them to a value. As of right now, they are null so that's what it means by 'Object or variable not set'.
You need the lines:
Set swExportData = swApp.GetExportFileData(1)
Set swModelDocExt = swModel.Extension
or something similar.
Thanks Alex, I updated the references and it worked perfectly! I've tried several macros that make a pdf and do something with it and this is the first time that generating a pdf has finally worked the way it was supposed to. Thanks for the help