5 Replies Latest reply on Jan 14, 2014 10:27 AM by Simon Turner

    Help: Export drawing to PDF, only certain sheets

    Chris Manger

      I am writing a macro to export all sheets except for one that's called "FLAT" to a PDF file.   I took some sample code from the API help.


      Everything works until I get to creating a new array called "varPrint".   I don't use arrays I suspect I am using that variable incorrectly.

       

       

      Option Explicit
      
      
      
      
      
          Dim swApp               As SldWorks.SldWorks
      
      
          Dim swModel             As SldWorks.ModelDoc2
      
          Dim swDrawDoc           As SldWorks.DrawingDoc
      
      
          Dim swModelDocExt       As SldWorks.ModelDocExtension
      
      
          Dim swExportPDFData     As SldWorks.ExportPdfData
      
      
          Dim boolstatus          As Boolean
      
      
          Dim filename            As String
      
      
          Dim lErrors             As Long
      
      
          Dim lWarnings           As Long
      
      
          Dim strSheetName(4)     As String
      
      
          Dim varSheetName        As Variant
      
          Dim varPrint(2)            As Variant
      
          Dim i                   As Integer
      
          Dim count               As Integer
      
      
      
      
      
      Sub main()
      
      
      
      
      
          ' Path to which to save PDF file of drawing
      
      
          filename = "C:\test.PDF"
      
      
      
          Set swApp = Application.SldWorks
      
      
          swApp.Visible = True
      
      
          ' Open specified drawing
      
      
          Set swModel = swApp.OpenDoc6("C:\test.SLDDRW", swDocDRAWING, swOpenDocOptions_Silent, "", lErrors, lWarnings)
      
      
      '    Set swModelDocExt = swModel.Extension
      
          Set swDrawDoc = swModel
          varSheetName = swDrawDoc.GetSheetNames
          count = swDrawDoc.GetSheetCount
          For i = 0 To count - 1
              If varSheetName(i) <> "FLAT" Then
                  varPrint(i) = varSheetName(i)
              End If
              'MsgBox varSheetName(i)
          Next i
      
          Set swExportPDFData = swApp.GetExportFileData(3)
      
      
      '    If swExportPDFData Is Nothing Then MsgBox "Nothing"
      '
          boolstatus = swExportPDFData.SetSheets(swExportData_ExportSpecifiedSheets, varPrint)
      '
      '
      boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportPDFData, lErrors, lWarnings)
      
      
      
      
      
      End Sub
      
      
        • Re: Help: Export drawing to PDF, only certain sheets
          Simon Turner

          I haven't tested this, but I have corrected the code:

           

          Option Explicit

          Option Compare Text

           

          Dim swApp               As SldWorks.SldWorks

          Dim swModel             As SldWorks.ModelDoc2

          Dim swDrawDoc           As SldWorks.DrawingDoc

          Dim swModelDocExt       As SldWorks.ModelDocExtension

          Dim swExportPDFData     As SldWorks.ExportPdfData

          Dim boolstatus          As Boolean

          Dim filename            As String

          Dim lErrors             As Long

          Dim lWarnings           As Long

          Dim strSheetName(4)     As String

          Dim varSheetName        As Variant

          Dim strPrint()          As String

          Dim nPrint              As Integer

          Dim varPrint            As Variant

          Dim i                   As Integer

          Dim count               As Integer

           

          Sub main()

           

              ' Path to which to save PDF file of drawing

              filename = "C:\test.PDF"

           

              Set swApp = Application.SldWorks

              swApp.Visible = True

           

              ' Open specified drawing

              Set swModel = swApp.OpenDoc6("C:\test.SLDDRW", swDocDRAWING, swOpenDocOptions_Silent, "", lErrors, lWarnings)

           

          '    Set swModelDocExt = swModel.Extension

           

              Set swDrawDoc = swModel

              varSheetName = swDrawDoc.GetSheetNames

              count = swDrawDoc.GetSheetCount

              nPrint = -1

              For i = 0 To count - 1

                  If varSheetName(i) <> "FLAT" Then

                      nPrint = nPrint + 1

                      Redim Preserve strPrint(nPrint)

                      strPrint(nPrint) = varSheetName(i)

                  End If

                  'MsgBox varSheetName(i)

              Next i

              varPrint = strPrint

           

              Set swExportPDFData = swApp.GetExportFileData(3)

           

           

          '    If swExportPDFData Is Nothing Then MsgBox "Nothing"

          '

              boolstatus = swExportPDFData.SetSheets(swExportData_ExportSpecifiedSheets, varPrint)

          '

          '

          boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportPDFData, lErrors, lWarnings)

           

          End Sub