Code Library
SolidWorks APIVBASolidWorks 2016+ · built-in VBA editorMIT license

Batch-export every SolidWorks drawing in a folder to PDF

The classic design-office time sink: forty drawings need to go out as PDFs and someone spends an afternoon on Save As. This macro walks a folder, opens every .SLDDRW read-only, exports all sheets of each to a PDF next to the original, closes it, and reports a pass/fail count at the end. Failures are listed in the Immediate window instead of stopping the run — one corrupt drawing shouldn't kill the batch.

Before you run it

  • SolidWorks with the VBA editor (Tools → Macro → New)
  • In the VBA editor, Tools → References → tick the two SolidWorks Type Libraries (usually pre-ticked)
  • Edit the FOLDER constant at the top before running

The code

GitHub
Option Explicit

' Batch-export every SOLIDWORKS drawing in a folder to PDF (all sheets).
' Edit FOLDER, then run main(). Failures print to the Immediate window
' (Ctrl+G) and the batch keeps going.

Const FOLDER As String = "C:\Drawings\"   ' must end with a backslash

Sub main()
    Dim swApp As SldWorks.SldWorks
    Set swApp = Application.SldWorks

    Dim swExport As SldWorks.ExportPdfData
    Set swExport = swApp.GetExportFileData(swExportPdfData)
    swExport.ViewPdfAfterSaving = False

    Dim fileName As String
    Dim exported As Long, failed As Long
    fileName = Dir$(FOLDER & "*.slddrw")

    Do While fileName <> ""
        Dim errs As Long, warns As Long
        Dim swModel As SldWorks.ModelDoc2
        Set swModel = swApp.OpenDoc6(FOLDER & fileName, swDocDRAWING, _
            swOpenDocOptions_Silent Or swOpenDocOptions_ReadOnly, "", errs, warns)

        If Not swModel Is Nothing Then
            Dim pdfPath As String
            pdfPath = FOLDER & Left$(fileName, Len(fileName) - 7) & ".pdf"

            ' Export every sheet, not just whichever one was active
            Dim swDraw As SldWorks.DrawingDoc
            Set swDraw = swModel
            swExport.SetSheets swExportData_ExportAllSheets, swDraw.GetSheetNames

            If swModel.Extension.SaveAs(pdfPath, 0, 0, swExport, errs, warns) Then
                exported = exported + 1
            Else
                failed = failed + 1
                Debug.Print "EXPORT FAILED: " & fileName & " (error " & errs & ")"
            End If
            swApp.CloseDoc swModel.GetTitle
        Else
            failed = failed + 1
            Debug.Print "COULD NOT OPEN: " & fileName & " (error " & errs & ")"
        End If

        fileName = Dir$()
    Loop

    swApp.SendMsgToUser exported & " PDF(s) exported, " & failed & " failed." & _
        vbNewLine & "Failures (if any) are in the Immediate window (Ctrl+G)."
End Sub

How it works

  • Dir$(FOLDER & "*.slddrw") starts a folder walk; each later Dir$() call returns the next match. Old-school VBA, zero dependencies.
  • OpenDoc6 with swOpenDocOptions_Silent Or swOpenDocOptions_ReadOnly opens without dialogs and without locking the file for writing — the batch never pops a question at you.
  • SetSheets swExportData_ExportAllSheets, GetSheetNames is the line most hand-rolled macros miss: without it a multi-sheet drawing exports only the sheet that was active when it was last saved.
  • Extension.SaveAs returns True/False and fills errs — the macro counts failures instead of trusting silence.
  • CloseDoc after every export keeps memory flat on big folders.

Gotchas & honest limits

  • The path in FOLDER must end with a backslashC:\Drawings\, not C:\Drawings.
  • Drawings whose references can't be found may open with empty views and export blank-looking PDFs — fix the references, don't blame the macro.
  • If constants like swDocDRAWING show as undefined, the SolidWorks type-library references aren't ticked (VBA editor → Tools → References).
  • PDM users: run this on a local working folder, not directly inside a vault view — checked-in files open read-only anyway, but local cache states confuse batches.

Goes deeper

Want this adapted to your shop — or built into a real tool?

Samples are the free 80%. The last 20% is the part I do for a living.

Get in touch
Home
Blog
Tools
Code
Email
LinkedIn
Résumé