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.

Concept illustration of part identity, assembly interfaces and verification
Concept illustration: approved parts → assembly → verification.

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.

Jump to GetComponents True versus False, component occurrences versus BOM quantity, or configuration differences.

Concept illustration linking a process diagram, connection graph and equipment assembly
Concept illustration: process diagram → connections → equipment assembly.

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. List components inside a subassembly

SOLIDWORKS GetComponents(True) vs GetComponents(False)

The argument is TopLevelOnly: True limits the list to direct children of the active assembly; False includes components inside subassemblies too. Neither setting makes the returned array an ordered tree. For a hierarchy, follow the component relationships explicitly; see the official GetChildren reference for recursive, one-level-at-a-time traversal.

Build the following practice case with all five component occurrences resolved and unsuppressed. Both sensor occurrences reference the same saved part and configuration. This is a hand-counted teaching fixture, not captured SOLIDWORKS output:

PracticeAssembly
  Pump-1
  Bracket-1
  Module-1 (subassembly)
    Sensor-1
    Sensor-2
Expected membership: GetComponents flags and traversal
Mode Expected result
True Three occurrences: Pump-1, Bracket-1 and Module-1.
False Five occurrences: the same three plus the two sensors inside Module-1. Module-1 itself remains a row.
Recursive traversal The same component membership in this resolved fixture, with the parent relationships recorded explicitly. Do not infer those relationships from flat-array row order.

Change only the Boolean argument in the macro above and compare the two row sets. Match the actual occurrence names shown by your practice assembly; the displayed names and order need not be identical to this teaching tree. If the membership differs from the hand count, inspect the active configuration and component states before exporting the list.

Why does a component list differ from a BOM quantity?

The five rows above include one subassembly occurrence and four part occurrences. The two sensor placements share a part file, so there are only three distinct part files in this fixture. Those are three different counts: component occurrences, part occurrences and unique part files.

Do not delete repeated sensor rows just because they reference the same file. That would lose a placement. Conversely, treating the subassembly row as another sensor would invent quantity. A released BOM also follows its chosen BOM type, exclusions, part-number settings and configuration rules. Reconcile with the BOM configuration tutorial instead of labeling this API list a verified manufacturing BOM.

Why does the list change with the assembly configuration?

The active assembly configuration and each subassembly occurrence’s referenced configuration both matter. Record both before comparing two runs. The official GetChildren reference explains that a suppressed subassembly can return an empty child array; it also warns that child order can change. An empty or reordered result is not enough to diagnose a missing file.

  1. Record the assembly name, active configuration and the affected subassembly occurrence’s referenced configuration.
  2. Compare occurrence membership, not line numbers. Mark suppressed, lightweight, unloaded or unresolved cases separately from the simple resolved fixture.
  3. Return to the known practice configuration and repeat the hand-counted check. Keep unresolved rows visible in the report instead of silently dropping them or changing model states to force a count.

If an AI drafted your macro, use the same checks: reviewing an AI-written SOLIDWORKS macro is separate from proving its output.

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

Long-tail reading examples reviewed on 6 October 2026 using the versioned references above; 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.

Use the component list in your next review

How to cite this article

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

FOR EQUIPMENT MANUFACTURERS AND DESIGN TEAMS

Talk to the engineering team.

Tell us what your team designs and which repetitive task you want to improve. A short, non-confidential description is enough to discuss a relevant demo. Arrange a private channel with MST before sharing company drawings or models.

Discuss your equipment design sales@mst-us.ai See the equipment assembly example
Operated by MOORE SOLUTION TECHNOLOGY PTE. LTD. · Singapore UEN 202315239E