SOLIDWORKS VBA Macro Code to Save Drawing as a PDF

Article by Samony Riyaz updated September 4, 2019


Usually when we want to speed up process tasks in SOLIDWORKS, we tend to search for Macros that can do the task for us so we can decrease our process time. A lot of these Macros are published online by various users, but be mindful as the code published is SPECIFIC to their machine and environment and might not be the same for you.

Let’s take a look at one particular Macro — Save SOLIDWORKS Drawing to PDF

  • Modify the macro to integrate it within your process workflow
  • Create a Macro button and a hotkey for the Macro

Sample Code:

Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swExportPDFData As SldWorks.ExportPdfData
Dim strFilename As String
Dim status As Boolean
Dim errors As Long, warnings As Long
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
status = swModel.Save3(swSaveAsOptions_e.swSaveAsOptions_Silent, errors, warnings)
'Export to PDF if it is a drawing
If (swModel.GetType = swDocDRAWING) Then
strFilename = swModel.GetPathName
strFilename = Left(strFilename, Len(strFilename) - 6) & "pdf"
Set swExportPDFData = swApp.GetExportFileData(1)
swModel.Extension.SaveAs strFilename, 0, 0, swExportPDFData, 0, 0
End If
End Sub

The above code was altered so that along with saving the Drawing file, it also exports a PDF of that drawing using the system default settings.

Integrate the Code:

Launch SOLIDWORKS and navigate to Tools > Macro > New

  • This brings up the Folder browser dialog prompting you to save the file.
    • If this macro is being shared with others within your network, make sure to save it in a Shared network location.

IF your coworkers are using a DIFFERENT version of SOLIDWORKS, or a different installation directory – It may cause reference errors

  • Workaround: Send them the code, so they can create the macro on their own local machine
  • Once you have saved the macro, a Visual Basic for Applications Dialog box will open with the default code below:
Dim swApp As Object
Sub main()
Set swApp = Application.SldWorks
End Sub

Replace the default code with the “Sample Code” shown above and Save.

Congratulations! You have just created a usable macro from the source code.

Running the Macro with a Quick Access Button:

Now to use the Save SOLIDWORKS Drawing to PDF macro efficiently you will need to customize a Macro button/hotkey within SOLIDWORKS:

  1. Create a custom macro button
    • Launch SOLIDWORKS and Create a New Drawing (New > Drawing (use any available template))
    • Right click on the Task bar > Customize > Commands Tab > Select the Macro Category
    • Drag the icon for macro button to a suitable location.
    • Beside Macro Textbox, Click the browse button > select the macro. If there is more than one method select the appropriate method from the list. Click OK.
    • Your new macro button is now usable.
  2. Create a Keyboard shortcut
    • Launch SOLIDWORKS and Create a New Drawing (New > Drawing (use any available template))
    • Right click on the Task bar > Customize > Keyboard Tab > Scroll to the very bottom until you see the row for the Macro button
    • Press any combination you want, in this case we will do (Ctrl + P) for Printing to PDF
    • Click Yes to Override, Click OK
    • Now Control P will save a PDF if a drawing file is open.
Related Links

Certified SOLIDWORKS Services available from Javelin

Javelin can help you to:

Samony Riyaz