US$0.00
0

SOLIDWORKS Macro to Save Each Drawing Sheet as a Separate PDF

Export every sheet of a SOLIDWORKS drawing to its own PDF file in one click.
SOLIDWORKS Macro to Save Each Drawing Sheet as a Separate PDF
Picture of Amen Jlili

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.

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

  1. Open SOLIDWORKS and the document described in the pre-requisites.
  1. 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.
  1. 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.

Need this adapted to your workflow?

We build custom SOLIDWORKS macros, add-ins and PDM automation.

Picture of Amen Jlili

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.

Leave a Comment

0
0
Your Cart
Your cart is emptyReturn to Shop