Description
- Exports every sheet of the active drawing to its own PDF.
- Files are saved next to the drawing as DrawingName – SheetName.pdf.
- Characters that are not allowed in file names are replaced automatically.
System Requirements
- SOLIDWORKS Version: SOLIDWORKS 2014 or newer
- Operating System: Windows 10 or later
Pre-Requisites
- The active document must be a saved drawing.
Results
- One PDF per sheet in the drawing folder.
- A message shows how many sheets were exported.
Steps to Set Up the Macro
- Open SOLIDWORKS and the document described in the pre-requisites.
- Create the macro
- Go to Tools > Macro > New, give the .swp file a name and save it. The VBA editor opens.
- Replace everything in the module with the code below.
- Run the macro
- Press F5 in the VBA editor, or use Tools > Macro > Run.
- For daily use, assign it to a toolbar button or keyboard shortcut via Tools > Customize.
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 https://bluebyte.biz
' Reviewed and updated by Blue Byte Systems Inc.
Option Explicit
' Exports every sheet of the active drawing to its own PDF file,
' saved next to the drawing as "<DrawingName> - <SheetName>.pdf".
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swPdfData As SldWorks.ExportPdfData
Dim vSheets As Variant
Dim sheetList(0) As String
Dim docPath As String
Dim outFolder As String
Dim baseName As String
Dim outFile As String
Dim errors As Long
Dim warnings As Long
Dim i As Long
Dim saved As Long
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then
MsgBox "Open a drawing before running this macro.", vbExclamation
Exit Sub
End If
If swModel.GetType <> swDocDRAWING Then
MsgBox "The active document must be a drawing.", vbExclamation
Exit Sub
End If
docPath = swModel.GetPathName
If docPath = "" Then
MsgBox "Save the drawing first. The PDFs are written to its folder.", vbExclamation
Exit Sub
End If
outFolder = Left(docPath, InStrRev(docPath, "\"))
baseName = Mid(docPath, InStrRev(docPath, "\") + 1)
baseName = Left(baseName, InStrRev(baseName, ".") - 1)
Set swDraw = swModel
Set swPdfData = swApp.GetExportFileData(swExportPdfData)
vSheets = swDraw.GetSheetNames
For i = 0 To UBound(vSheets)
sheetList(0) = vSheets(i)
swPdfData.SetSheets swExportData_ExportSpecifiedSheets, sheetList
outFile = outFolder & baseName & " - " & CleanName(CStr(vSheets(i))) & ".pdf"
If swModel.Extension.SaveAs(outFile, swSaveAsCurrentVersion, swSaveAsOptions_Silent, _
swPdfData, errors, warnings) Then
saved = saved + 1
Else
MsgBox "Could not export sheet '" & vSheets(i) & "' (error " & errors & ").", vbExclamation
End If
Next
MsgBox saved & " of " & (UBound(vSheets) + 1) & " sheet(s) exported to " & outFolder, vbInformation
End Sub
' Replaces characters that are not allowed in Windows file names
Private Function CleanName(ByVal s As String) As String
Dim bad As Variant
For Each bad In Array("\", "/", ":", "*", "?", """", "<", ">", "|")
s = Replace(s, bad, "_")
Next
CleanName = s
End Function
Code Review Notes
We reviewed this macro before publishing it here. Changes from the original version:
- The original saved PDFs to SOLIDWORKS’ working folder instead of the drawing folder. Files now go next to the drawing with a clear name.
- The sheet list is now passed in the format the API expects.
Need to customize the macro?
Contact us and we will adapt this macro to your workflow, or build a complete SOLIDWORKS or PDM automation for your team.