Rename Assembly Components Example (VBA)
This example shows how to rename assembly components.
'---------------------------------------
'
' Preconditions: Assembly document is open.
'
' Postconditions: Assembly component names are changed
by
' appending
123 to the end of the names.
'
'---------------------------------------
Option Explicit
Public Enum swUserPreferenceToggle_e
swExtRefUpdateCompNames
= 18
End Enum
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
ChildCount As
Integer
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.GetRootComponent
bOldSetting
= swApp.GetUserPreferenceToggle(swExtRefUpdateCompNames)
swApp.SetUserPreferenceToggle swExtRefUpdateCompNames,
False
Children
= swRootComp.GetChildren
ChildCount
= UBound(Children)
For
i = 0 To ChildCount
Set
swChild = Children(i)
'
Changing component name requires component to be selected
bRet
= swChild.Select2(False, 0)
OldName
= swChild.Name2
NewName
= OldName & " 123"
Debug.Print
"OldName = " + OldName
Debug.Print
"NewName = " + NewName
Debug.Print
""
swChild.Name2 = NewName
Next
i
swApp.SetUserPreferenceToggle swExtRefUpdateCompNames,
bOldSetting
End Sub
'---------------------------------------