ENGINEERING RESEARCH · CAD ENGINEERS LEARNING AUTOMATION

SOLIDWORKS API Tutorial: List Assembly Components with VBA

Read component occurrences and referenced configurations from a small assembly, compare top-level and nested results, and verify the output by hand.

Use the SOLIDWORKS API to read component occurrences from the active assembly configuration, then report each occurrence name and referenced configuration. This tutorial builds on the first VBA macro and produces an inspectable component list in the VBA Immediate window.

1. Prepare a small assembly you can count by hand

Use a practice assembly containing two occurrences of the same saved part. Name the assembly and part clearly. Open the intended assembly configuration and note the occurrence names visible in the FeatureManager tree.

Record the SOLIDWORKS version, assembly filename and configuration. Start with a normally opened, resolved sample; lightweight, suppressed, virtual or unloaded components deserve separate cases once the simple exercise is understood.

2. Read the component array

Create or edit a VBA macro through the macro tools. Open the Immediate window in the VBA editor. This example uses the SOLIDWORKS type library normally referenced by a SOLIDWORKS-created VBA macro; verify that reference if the editor reports an undefined type. The following example attaches to the current SOLIDWORKS application and reads the active document; it does not open or save a model.

Option Explicit

Sub main()
    On Error GoTo ReadFailed
    Dim app As SldWorks.SldWorks
    Dim model As SldWorks.ModelDoc2
    Dim assembly As SldWorks.AssemblyDoc
    Dim component As SldWorks.Component2
    Dim items As Variant
    Dim index As Long

    Set app = Application.SldWorks
    Set model = app.ActiveDoc
    If model Is Nothing Then
        MsgBox "Open a practice assembly first."
        Exit Sub
    End If
    If model.GetType <> 2 Then
        MsgBox "The active document must be an assembly."
        Exit Sub
    End If

    Debug.Print "Assembly: " & model.GetTitle
    Debug.Print "Configuration: " & _
        model.ConfigurationManager.ActiveConfiguration.Name

    Set assembly = model
    items = assembly.GetComponents(False)
    If IsEmpty(items) Then
        Debug.Print "No component array was returned."
        Exit Sub
    End If

    For index = LBound(items) To UBound(items)
        Set component = items(index)
        If Not component Is Nothing Then
            Debug.Print component.Name2 & vbTab & _
                component.ReferencedConfiguration
        End If
    Next index
    Exit Sub

ReadFailed:
    MsgBox "Component read stopped: " & Err.Description
End Sub

GetType = 2 identifies an assembly document. Passing False to GetComponents requests top-level and child components of the active assembly configuration. The returned order must not be treated as the assembly hierarchy.

3. Compare occurrences with files and configurations

Run the macro and compare its rows with your two-occurrence sample. Two placements of one part are two component occurrences. Their names may differ while their referenced part configuration is the same.

Next, manually select another existing assembly configuration and run the macro again. Label the two outputs before comparing them. Components are interpreted in configuration context; an output difference is something to investigate against the tree, not automatically an API failure.

4. Extend the test to a nested subassembly

Use a second practice assembly with a small subassembly. Compare output with GetComponents(True) and then GetComponents(False). Record which rows belong to the top level and which appear only when child components are included.

If you need parent/child structure, use an explicit traversal through the documented component relationships. Sorting a flat list or relying on the order returned by GetComponents cannot reconstruct that structure reliably. The official traversal example shows a separate recursive approach.

5. Check the failure cases and the limits of the result

  • With no active document, the macro should ask for an assembly.
  • With a part or drawing active, it should stop before requesting components.
  • With the two-occurrence sample, each expected occurrence should be identifiable in the output.
  • With a nested sample, the top-level flag should make an explainable difference.
  • Investigate suppressed, lightweight, virtual and unloaded cases separately; do not silently treat an incomplete report as a released BOM.

This list does not verify mates, placement, interference, rebuild health or drawing/BOM configuration rules. Continue with BOM part numbers and configurations and mate verification when those are the actual task.

Primary references

Documentation-reviewed on 17 September 2026; this macro has not been executed in SOLIDWORKS for this publication. Run the small verification cases in your own practice files before relying on the output.

How to cite this article

MST Engineering Intelligence. “SOLIDWORKS API Tutorial: List Assembly Components with VBA.” MST Engineering Intelligence. Updated 2026-09-16. https://mst-us.ai/solidworks-api-list-assembly-components/

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