ENGINEERING RESEARCH · CAD ENGINEERS LEARNING AUTOMATION

SOLIDWORKS API Tutorial: Your First Read-Only VBA Macro

Create a small VBA macro that reads the active document title and path, then verify saved, unsaved and no-document cases.

Direct answer: start SOLIDWORKS API automation with a macro that reads the active document and reports its title and saved path. Check the no-document case before calling document methods. This gives you a small, observable first task before attempting edits, component insertion or assembly generation.

This walkthrough is for desktop SOLIDWORKS on Windows with its VBA macro environment available. The example is original teaching code reviewed against documented API methods; it has not been executed in a SOLIDWORKS session for this article. Verify it on your installed version using a disposable sample document.

Step 1: create a separate learning macro

Use SOLIDWORKS’ macro tools to create a new VBA macro, save it in your own test folder, and open the editor. The official macro-editing guide explains how to open and edit a VBA macro. Menu presentation can differ by version and installation.

Replace the generated module’s example procedure with the code below. It uses late-bound objects so the teaching example does not need additional type-library declarations. For a maintained application, use the binding and reference strategy appropriate to your supported versions.

Step 2: read the active document

Option Explicit

Sub main()
    On Error GoTo Failed
    Dim swApp As Object
    Dim model As Object
    Dim savedPath As String

    Set swApp = Application.SldWorks
    Set model = swApp.ActiveDoc

    If model Is Nothing Then
        MsgBox "Open a sample document first."
        Exit Sub
    End If

    savedPath = model.GetPathName
    If Len(savedPath) = 0 Then
        savedPath = "(not saved yet)"
    End If

    MsgBox "Title: " & model.GetTitle & vbCrLf & _
           "Path: " & savedPath
    Exit Sub

Failed:
    MsgBox "Read failed: " & Err.Description
End Sub

Application.SldWorks refers to the host SOLIDWORKS application in this macro context. ActiveDoc supplies the active document object. GetTitle and GetPathName read document identity; this example contains no save, rebuild, delete or geometry-edit call.

Method references: IModelDoc2.GetTitle and IModelDoc2.GetPathName. Consult the API help installed with your version when checking compatibility.

Step 3: verify three outcomes

  1. No open document: run the macro. Expect the instruction to open a sample, without a document-method error.
  2. New unsaved sample: create a blank sample document and run it. Expect the displayed document title and the explicit unsaved-path label.
  3. Saved sample: save your test document normally, then run the macro. Compare the displayed path with the file you saved.

Record your SOLIDWORKS version, sample state and actual result. A compilation or execution error is a failed exercise until investigated; the code appearing plausible is not evidence that it ran.

Common first-macro problems

  • Running inside the wrong application: this example expects the SOLIDWORKS VBA host, not an Excel or standalone script context.
  • Two procedures named main: replace the generated example instead of pasting a second copy into the same module.
  • Copied HTML entities: copy the rendered code, where the concatenation operator is an ampersand, rather than the page’s HTML source.
  • Unreviewed recorded commands: begin with the small read-only module instead of retaining unrelated recorded edits.

What to learn next

Before automating modifications, define the allowed inputs, output file, expected CAD state and failure behavior. Read how an LLM can interact with SOLIDWORKS and how to diagnose assembly mates. The research library connects these exercises to engineering automation.

Next: read the components of an assembly

Continue with the assembly component-list tutorial. It adds document-type checks, occurrence names, referenced configurations and a top-level versus nested comparison.

Scope: this introductory macro reads identity only. It is not a tested MST execution worker, a general AI plug-in or a verified assembly-generation example.

How to cite this article

MST Engineering Intelligence. “SOLIDWORKS API Tutorial: Your First Read-Only VBA Macro.” MST Engineering Intelligence. Updated 2026-09-16. https://mst-us.ai/solidworks-api-first-read-only-macro/

CUSTOMERS · PARTNERS · INVESTORS

Talk to the engineering team.

Bring a non-confidential P&ID question, pilot boundary or technical diligence request. Protected engineering material is exchanged only after identity, NDA and scope review.

Request a live challenge [email protected] Partners & investors
Operated by MOORE SOLUTION TECHNOLOGY PTE. LTD. · Singapore UEN 202315239E