All writing
Automation3 min read

Your first SolidWorks macro (VBA), explained line by line

The SolidWorks API is the highest-leverage automation surface most design offices never touch. Here's a real macro — save the active drawing as PDF — with every line explained.

Engineer working at a CAD workstation with a light pen

Every design office has a task like this: forty drawings need to go out as PDFs, and somebody — a degreed engineer, usually — spends an afternoon doing File → Save As → PDF → OK forty times. SolidWorks has had a full automation API since the 90s. It's sitting under Tools → Macro, it speaks VBA, and the license you already own includes all of it. Let's write the classic first macro: save the active drawing as a PDF, correctly.

Record one first (then throw it away)

Tools → Macro → Record, do the Save-As by hand, stop recording, and open what it wrote. Recorded macros are ugly — hardcoded paths, magic numbers, dead lines — but they answer the hardest beginner question: what is the API call for the thing I just did? Record to discover, then rewrite clean. Here's the clean version:

Option Explicit

Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swExport As SldWorks.ExportPdfData
    Dim pdfPath As String
    Dim errs As Long, warns As Long

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc

    If swModel Is Nothing Then
        swApp.SendMsgToUser "Open a drawing first."
        Exit Sub
    End If

    If swModel.GetType <> swDocDRAWING Then
        swApp.SendMsgToUser "This macro only works on drawings."
        Exit Sub
    End If

    ' same folder, same name, .pdf extension
    pdfPath = Left$(swModel.GetPathName, _
                    Len(swModel.GetPathName) - 6) & "pdf"

    Set swExport = swApp.GetExportFileData(swExportPdfData)
    swExport.ViewPdfAfterSaving = False

    swModel.Extension.SaveAs pdfPath, 0, 0, swExport, errs, warns

    If errs <> 0 Then
        swApp.SendMsgToUser "Export failed, error code " & errs
    End If
End Sub

What each part is doing

  • `Application.SldWorks` — the running SolidWorks instance. Every macro starts by grabbing this; it's the root of the whole object model.
  • `ActiveDoc` → `ModelDoc2` — whatever document is open in front of the user. ModelDoc2 is the workhorse interface: parts, assemblies, and drawings all present it.
  • The two guard clauses — the difference between a tool and a booby trap. ActiveDoc is Nothing when no file is open, and GetType stops someone running a drawing macro on a part. Recorded macros have neither; production macros always should.
  • `GetPathName` surgery — the saved file's full path; chop SLDDRW (6 chars) off the end and append pdf. Note this is empty for never-saved documents — guard for that too as homework.
  • `GetExportFileData(swExportPdfData)` — the options object for PDF export; this is where you'd set "all sheets" (SetSheets) or turn off the annoying auto-open of the finished PDF.
  • `Extension.SaveAs` — the modern save call. The errs/warns out-parameters are how it tells you what went wrong; ignoring them is how "the macro worked" and "there's no PDF" happen at the same time.

The pitfalls everyone hits

1) If SldWorks.SldWorks doesn't autocomplete, the type library reference is missing — in the VBA editor, Tools → References, tick the SolidWorks type libraries. 2) Constants like swDocDRAWING and swExportPdfData live in those libraries; without the reference they're just undefined names. 3) Save macros as .swp somewhere on the network outside any vault folder, or PDM will version-control your tooling into chaos.

Run it, wire it to a button

Tools → Macro → Run and pick the .swp. If it earns its keep, give it a toolbar button (Tools → Customize → Commands → Macro) — adoption in an office is directly proportional to how little the macro looks like programming.

Where this goes next

The jump from the active document to every file in a folder — loop the directory, OpenDoc6, export, CloseDoc — is maybe fifteen more lines, and it's the one that turns the forty-PDF afternoon into a coffee break — the finished batch version is in my free code library, ready to download. The same pattern batch-exports STEP files, fixes title blocks, and pulls BOMs into Excel — where it hands off to the Python side of shop automation. And if you'd rather skip to the working tool, I build these.

Nobody in the office will care that it's forty lines of VBA. They'll care that drawings now leave the building in one click.

Muerus Rodrigues

Applications Engineer

Get in touch

Keep reading

Home
Blog
Tools
Code
Email
LinkedIn
Résumé