US$0.00
0

SOLIDWORKS Macro to Save Drawings as PDF (Toolbar Button Integration)

Description

This macro is a one-action solution that exports the active SolidWorks drawing as a PDF file. It combines all the drawing sheets into one single PDF file. This utility is made to be paired with a custom button on the SolidWorks toolbar as a quick one-click way for engineers to create a SolidWorks PDF directly from the drawing environment. The SOLIDWORKS macro to save drawings as PDF makes this process even more efficient by automating the export with a single command.

System Requirements

  • SolidWorks Version: SolidWorks 2014 or newer
  • Operating System: Windows 7 or later

Pre-Conditions

  • The active document must be a saved drawing file (.SLDDRW). 
  • The drawing file must have a valid path (not saved to read-only). 
  • This macro is designed to exclusively work with drawing documents and will not work with part or assembly files.

Results

  • Every sheet contained in the active drawing is exported into a single PDF file. 
  • The PDF file is automatically saved in the same directory as the original drawing file, and uses the original drawing name with the extension of .PDF. 
  • A message box indicates the status of the operation (Completion status or Failure).

Steps to Set Up the Macro

To create the one-click PDF export functionality:

  1. Create the Macro File: In SolidWorks, choose Tools > Macro > New….
  2. Save the File: Give the macro a name (e.g.,  ExportDrawingPDF.swp) and save it to a location you intend to keep it. The VBA editor will launch immediately after.
  3. Insert the Code: Copy the code from the “VBA Macro Code” below and paste it in the VBA editor, erasing the prefilled content.
  4. Save and Close: Save in the VBA editor.
  5. Create the Button on the Toolbar:

VBA Macro Code

' Disclaimer:
' The code provided should be used at your own risk.  
' Blue Byte Systems Inc. assumes no responsibility for any issues or damages that may arise from using or modifying this code.  
' For more information, visit [Blue Byte Systems Inc.](https://bluebyte.biz).

Option Explicit

' --------------------------------------------------------------------------
' Main subroutine to save the active drawing as a PDF file
' --------------------------------------------------------------------------
Sub main()

    ' Declare and initialize necessary SolidWorks objects
    Dim swApp As SldWorks.SldWorks             ' SolidWorks application object
    Dim swModel As SldWorks.ModelDoc2          ' Active document object (drawing)
    Dim swModelDocExt As SldWorks.ModelDocExtension ' Document extension object for saving operations
    Dim swExportData As SldWorks.ExportPdfData ' PDF export data object
    Dim boolstatus As Boolean                  ' Boolean status to capture operation results
    Dim filename As String                     ' String to hold the file path of the drawing
    Dim lErrors As Long                        ' Error variable for save operation
    Dim lWarnings As Long                      ' Warning variable for save operation

    ' Initialize SolidWorks application and get the active document
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc

    ' Check if there is an active document open
    If swModel Is Nothing Then
        MsgBox "No current document. Please open a drawing and try again.", vbCritical, "No Active Document"
        End
    End If

    ' Check if the active document is a drawing
    If swModel.GetType <> swDocDRAWING Then
        MsgBox "This macro only works on drawings. Please open a drawing and try again.", vbCritical, "Invalid Document Type"
        End
    End If

    ' Get the document extension object for saving operations
    Set swModelDocExt = swModel.Extension

    ' Get the export PDF data object to specify PDF export options
    Set swExportData = swApp.GetExportFileData(swExportPDFData)

    ' Get the file path of the active drawing
    filename = swModel.GetPathName

    ' Check if the drawing has been saved previously (it must have a valid file path)
    If filename = "" Then
        MsgBox "Please save the file first and try again.", vbCritical, "File Not Saved"
        End
    End If

    ' Generate the PDF file path by changing the file extension to .PDF
    filename = Strings.Left(filename, Len(filename) - 6) & "PDF"

    ' Set the export option to include all sheets in the PDF file
    boolstatus = swExportData.SetSheets(swExportData_ExportAllSheets, 1)

    ' Save the drawing as a PDF file using the specified filename and options
    boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportData, lErrors, lWarnings)

    ' Check if the save operation was successful and display a message box
    If boolstatus Then
        MsgBox "Save as PDF successful!" & vbNewLine & filename, vbInformation, "Save Successful"
    Else
        MsgBox "Save as PDF failed. Error code: " & lErrors, vbExclamation, "Save Failed"
    End If

End Sub

Macro

You can download the macro from here

Need a Macro Developed to Improve Your Custom Workflow?

Contact us to either customize this macro or build completely new solutions to satisfy your specific workflow needs precisely.

Author

Amen Jlili

Amen Jlili is the founder and technical director of Blue Byte Systems Inc., a software company in Vancouver, Canada, specializing in automating SOLIDWORKS and PDM. With over a decade of experience, he has authored several courses and open-source frameworks related to the SOLIDWORKS API. His leadership ensures that Blue Byte Systems prioritizes customer satisfaction and delivers high-quality software and CAD design solutions.
0
    0
    Your Cart
    Your cart is emptyReturn to Shop
    ×