4 Replies Latest reply on Apr 3, 2013 9:24 AM by Brian Mick

    Save only certain sheets as a PDF via a Macro

    Brian Mick



      First post, hi everyone!


      I've been reading these forums for a while, and I have many macros that do great things, all by reading your posts. Thanks for that.


      Here is what I need help with:


      1. I work mostly with sheet metal, so most of my drawings have the flat pattern with no bend lines shown as the last page for exporting as a DXF (already a macro, thanks guys )

      2. We always name that page something with "FLAT" in the name.  Sometimes it's "Flat Pattern", sometimes "Flat -2" sometimes just "Flat".

      3. I want to export all pages as a *single* PDF that do NOT contain "FLAT" in their name.

      4. If there is no "FLAT" sheet, then export all sheets to a *single* PDF.

      5. If there are multiple sheets that contain "FLAT", export none of them.


      For the sake of simplicity, lets just save it to my desktop.  I already have the macro to find the proper directory based on custom properties.


      The PDF file name should be the same as the drawing name.


      Any suggestions?


      Thanks in advance!


      EDIT: As a get-by measure, is there any way to display the sheet selection dialog box that normally appears when saving a drawing as a PDF?


      EDIT 2: Added note that all the sheets not named some form of "flat" should be collectively saved as a single PDF file.

        • Re: Save only certain sheets as a PDF via a Macro
          Ivana Kolin

          If not UCase(swSheet.GetName) Like "*FLAT*" Then


          ' no flat in the name, do export



          End If

            • Re: Save only certain sheets as a PDF via a Macro
              Brian Mick



              Thanks for your quick reply.  I'd like to save them all as one file, and I'm not sure how to use the SaveAs functions to do that when I want to specify the sheet.  Can you help me with that?





                • Re: Save only certain sheets as a PDF via a Macro
                  Ivana Kolin

                  Option Explicit


                  Sub main()



                      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()      As String

                      Dim varSheetName        As Variant


                      ' Path to which to save PDF file of drawing

                      filename = "C:\xxxxx.PDF"


                      Set swApp = Application.SldWorks

                      swApp.Visible = True


                      Set swModel = swApp.ActiveDoc

                      Set swDrawDoc = swModel

                      Set swModelDocExt = swModel.Extension


                      Set swExportPDFData = swApp.GetExportFileData(swExportDataFileType_e.swExportPDFData)


                      ReDim strSheetName(0)

                      Dim s As Variant

                      For Each s In swDrawDoc.GetSheetNames

                          If Not UCase(s) Like "*FLAT*" Then

                              strSheetName(UBound(strSheetName)) = s

                              ReDim Preserve strSheetName(UBound(strSheetName) + 1)

                          End If

                      Next s


                      varSheetName = strSheetName

                      If swExportPDFData Is Nothing Then MsgBox "Nothing"

                      boolstatus = swExportPDFData.SetSheets(swExportData_ExportSpecifiedSheets, varSheetName)


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


                  End Sub