Hide Table of Contents

Rename Assembly Components Example (VBA)

This example shows how to rename assembly components.

' Preconditions:
' 1. Open an assembly.
' 2. Open the Immediate window.
' Postconditions:
' 1. Appends 123 to the end of each assembly component's
'    name.
' 2. Examine the FeatureManager design tree and the
'    Immediate window.
Option Explicit
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swConfigMgr As SldWorks.ConfigurationManager
    Dim swConfig As SldWorks.Configuration
    Dim swRootComp  As SldWorks.Component2
    Dim Children As Variant
    Dim swChild As SldWorks.Component2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swSelData As SldWorks.SelectData
    Dim ChildCount As Long
    Dim OldName As String
    Dim NewName As String
    Dim bOldSetting As Boolean
    Dim bRet As Boolean
    Dim i As Long
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swConfigMgr = swModel.ConfigurationManager
    Set swConfig = swConfigMgr.ActiveConfiguration
    Set swRootComp = swConfig.GetRootComponent3(True)
    bOldSetting = swApp.GetUserPreferenceToggle(swExtRefUpdateCompNames)
    swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, False
    Children = swRootComp.GetChildren
    ChildCount = UBound(Children)
    Set swSelMgr = swModel.SelectionManager
    Set swSelData = swSelMgr.CreateSelectData
    For i = 0 To ChildCount
        Set swChild = Children(i)
        ' Changing component name requires component to be selected
        bRet = swChild.Select4(False, swSelData, False)
        OldName = swChild.Name2
        NewName = OldName & " 123"
        Debug.Print "Old name = " + OldName
        Debug.Print "New name = " + NewName
        Debug.Print ""
        swChild.Name2 = NewName
    Next i
    swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, bOldSetting
End Sub

Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

Subject:   Feedback on Help Topics
Page:   Rename Assembly Components Example (VBA)
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:


We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again

Web Help Content Version: API Help (English only) 2015 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.