Oli Sparrow

Object variable or With block variable not set

Discussion created by Oli Sparrow on Feb 24, 2020
Latest reply on Feb 24, 2020 by Peter Brinkhuis

Hi all,


I've been using the below VB code for exporting named PDF and DXF. Now moved to a new business and no longer working, and can't quite figure out why?


Highlighted line in debug in red below.


Can anyone shed any light?



Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDrawModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swCustPropMgr As CustomPropertyManager
Dim swView As SldWorks.View
Dim swExportPDFData As SldWorks.ExportPdfData
Dim sFileName As String
Dim ValOut As String
Dim ResolvedValOut(1) As String
Dim wasResolved As Boolean
Dim nErrors As Long
Dim nWarnings As Long

Set swApp = Application.SldWorks
Set swDrawModel = swApp.ActiveDoc

' Check to see if a drawing is loaded.
If swDrawModel Is Nothing Then
MsgBox "There is no active drawing document"
Exit Sub
End If

If swDrawModel.GetType <> swDocDRAWING Then
MsgBox "Open a drawing first and then TRY again!"
Exit Sub
End If

If swDrawModel.GetPathName = "" Then
MsgBox "Save the drawing first and then TRY again!"
Exit Sub
End If

Set swDraw = swDrawModel
Set swView = swDraw.GetFirstView
Set swView = swView.GetNextView

' Determine if there is any view
If swView Is Nothing Then
MsgBox "Insert a View first and then TRY again!"
Exit Sub
End If

Set swModel = swView.ReferencedDocument
Set swCustPropMgr = swModel.Extension.CustomPropertyManager(swView.ReferencedConfiguration)
swCustPropMgr.Get5 "PROFILE ORDER CODE", False, ValOut, ResolvedValOut(0), wasResolved
swCustPropMgr.Get5 "PROFILE NAME", False, ValOut, ResolvedValOut(1), wasResolved

'Get and set file name
sFileName = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\")) & ResolvedValOut(0) & " - " & ResolvedValOut(1)
Set swExportPDFData = swApp.GetExportFileData(1)
swExportPDFData.SetSheets swExportData_ExportCurrentSheet, ""
swExportPDFData.ViewPdfAfterSaving = False

swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swDxfMultiSheetOption, swDxfActiveSheetOnly

'Save as DXF
swDraw.Extension.SaveAs sFileName & ".DXF", 0, 0, Nothing, nErrors, nWarnings

'Save as PDF
swDraw.Extension.SaveAs sFileName & ".PDF", 0, 0, swExportPDFData, nErrors, nWarnings

End Sub