AnsweredAssumed Answered

Macro Debug Help

Question asked by Eric Frissell on Dec 6, 2019
Latest reply on Dec 6, 2019 by Eric Frissell

Hi guys, I'm writing a macro to automate some 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

'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