4 Replies Latest reply on Jan 6, 2017 5:54 PM by bill bothe

    saveas pdf vba api crashes

    Rich Dobleman

      Wanted to share my experience with this vba macro because I could find nothing about it.  Briefly, the macro loops through many parts one at a time:  it opens, modifies a model and drawing and outputs several formats, including pdf, then closes them and loops to the next part.

       

      The problem:  solidworks was sometimes crashing at the "modeldocextension.saveas" (to pdf) api call.  It turns out the problem was eliminated when I changed the layer set to "will not print" to "will print" in the drawing (did this manually by clicking on the layer properties button, not programmatically).  This behavior is confirmed in another macro which works totally differently.  Note other conditions: only an annotation was on the layer, not attached to anything (or it is attached to the sheet view), and it is hidden, on a standard drawing fully rebuilt, model loaded and rebuilt, running 2016 service pack 5 on windows 7 pc.  Hope this helps the next person.

        • Re: saveas pdf vba api crashes
          Deepak Gupta

          Never had issues like that. Can you share the macro and some sample drawings to verify the issue.

           

          May be layer can be set to print and back to not print via macro itself.

            • Re: saveas pdf vba api crashes
              bill bothe

              First, the original post from Rich Dobleman is me: I had this login from before we switched to network licensing.

              Here is the procedure that saves the pdf - pretty straight forward but not standalone.  The cad files which had the trouble should be attached.  The drawing is "fixed", but use the layer manager and change the "macro" layer to "will not print" and try the api call.

               

              Function SavePDF(ByRef swDoc As SldWorks.ModelDoc2, ByRef swDrw As SldWorks.DrawingDoc, ByRef swApp As SldWorks.SldWorks, _
                  blOpenPdf As Boolean, Optional sName As String = "", Optional blOverWrite As Boolean = True, _
                  Optional iWhichSheets As swExportDataSheetsToExport_e = swExportData_ExportSpecifiedSheets, Optional iShtNum As Integer = 0, _
                  Optional ByRef swExportPDFData As SldWorks.ExportPdfData) As Boolean 
              '
              Debug.Print "SavePDF: start, timer = " & Timer,
              On Error GoTo Error1
              GoTo ErrCont1
              Error1:
              Call GenErrHandler(Err)
              Err.Clear
              Resume Next
              ErrCont1:
              '
              Dim sPDFpath As String
              Dim lSheetResult As Long
              Dim sSheetName As String
              Dim varSheetName      As Variant
              Dim sPDFname As String
              Dim strRev As String
              Dim boolStatus As Boolean
              Dim nErrors As Long
              Dim nWarnings As Long
              Dim swDrw4pdf As SldWorks.ModelDoc2
              Dim swModelDocExt      As SldWorks.ModelDocExtension
              Dim iAttr As Integer

              Set swDrw4pdf = swDrw

              Set swModelDocExt = swDrw4pdf.Extension

              If swExportPDFData Is Nothing Then Set swExportPDFData = swApp.GetExportFileData(1) '161228'  Err.Raise 3489, "!!!!!SavePDF", ".. did

              not receive swexportpdfdata object, cannot save pdf.": GoTo Exit1

              varSheetName = swDrw.GetSheetNames

              If iWhichSheets = swExportData_ExportAllSheets Then
                  boolStatus = swExportPDFData.SetSheets(swExportData_ExportAllSheets, Nothing)
              Else
                  boolStatus = swExportPDFData.SetSheets(swExportData_ExportSpecifiedSheets, varSheetName)
              End If
              If boolStatus = False Then Err.Raise 2098, "!!!!!savepdf", "..failed to set sheets": GoTo Exit1

              swExportPDFData.ExportAs3D = False
              swExportPDFData.ViewPdfAfterSaving = blOpenPdf

              If sName = "" Then ' then get the name from swdoc
                  sPDFname = GetFilePathNoExt(swDoc.GetPathName())
                  strRev = GetACustomProp2(swDrw, "Revision")

                  If strRev <> "" Then
                      sPDFname = sPDFname & "rev" & Right(strRev, 2)
                  End If
                 
              Else
                  sPDFname = GetFileNameOnly(sName)
                  sPDFpath = GetFilePath(sName)
              End If

              If Len(sPDFpath) < 2 Then sPDFpath = GetFilePath(swDoc.GetPathName())
              sPDFname = sPDFpath & sPDFname

              sPDFname = sPDFname & ".pdf"

              If blOverWrite = False Then
                  sPDFname = FileNameCheckorMakeUnique(sPDFname)
              End If

              On Error Resume Next '161228'check if file already exists
              iAttr = GetAttr(sPDFname)
              If Err.Number = 0 Then
                  Debug.Print vbCrLf & "<<<<<<<<<<<<  pdf already exists  >>>>>>>>>>>>>>>>>>"
                  Kill sPDFname
              End If
              On Error GoTo Error1

              boolStatus = swModelDocExt.SaveAs(sPDFname, swSaveAsCurrentVersion, swSaveAsOptions_Silent, swExportPDFData, nErrors, nWarnings)

              Debug.Print "  SavePDF: true if saved as PDF=" & boolStatus & ",  as " & sPDFname, "errors, warnings: " & nErrors, nWarnings
              '
              Exit1:
              On Error Resume Next
              Erase varSheetName
              Set swExportPDFData = Nothing
              Set swDrw4pdf = Nothing
              SavePDF = boolStatus
              Set swModelDocExt = Nothing
              '
              Debug.Print "SavePDF: end"

              End Function