Code:
'***********************************************
' Copy below code to a module
Public oChangeMaterial As clsChangeMaterialPublic Sub CreateComboBox()
' Set a reference to the active document.
' This assumes that a part document is active.
Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveDocument
Dim oCommandMgr As CommandManager
Set oCommandMgr = ThisApplication.CommandManager
' Set a reference to the collection of ControlDefinitions
Dim oControlDefs As ControlDefinitions
Set oControlDefs = oCommandMgr.ControlDefinitions
On Error Resume Next
' Create a combobox definition
Dim oComboBoxDef As ComboBoxDefinition
Set oComboBoxDef = oControlDefs.Item("MaterialsComboBox")
If oComboBoxDef Is Nothing Then
Set oComboBoxDef = oControlDefs.AddComboBoxDefinition("Materials", "MaterialsComboBox", kNonShapeEditCmdType, 125, , "Materials Combo", "Materials")
' Set a reference to the materials collection.
Dim oMaterials As Materials
Set oMaterials = oDoc.Materials
Dim oMaterial As Material
For Each oMaterial In oMaterials
' Add material names to the combo box definition
oComboBoxDef.AddItem (oMaterial.Name)
Next
' Set a reference to the Part Standard toolbar
Dim oPartFeatureToolbar As CommandBar
Set oPartFeatureToolbar = ThisApplication.UserInterfaceManager.CommandBars.Item("PMxPartFeatureCmdBar")
' Add a combo box control to the toolbar
Dim oComboBoxControl As CommandBarControl
Set oComboBoxControl = oPartFeatureToolbar.Controls.AddComboBox(oComboBoxDef,1)
oPartFeatureToolbar.Visible = True
End If
' Clear current selection
oComboBoxDef.ListIndex = 0
Set oChangeMaterial = New clsChangeMaterial
oChangeMaterial.Initialize
End Sub
'*************************************************************
' The declarations and functions below need to be copied into
' a class module whose name is "clsChangeMaterial". The name
' can be changed but you'll need to change the declaration in
' the calling function "CreateComboBox" to use the new name.
Option Explicit
Private WithEvents oComboBoxDef As ComboBoxDefinition
Public Sub Initialize()
Dim oCommandMgr As CommandManager
Set oCommandMgr = ThisApplication.CommandManager
' Set a reference to the collection of ControlDefinitions
Dim oControlDefs As ControlDefinitions
Set oControlDefs = oCommandMgr.ControlDefinitions
' Set a reference to the "MaterialsComboBox" combo box definition
Set oComboBoxDef = oControlDefs.Item("MaterialsComboBox")
End Sub
Private Sub oComboBoxDef_OnSelect(ByVal Context As NameValueMap)
If oComboBoxDef.ListIndex = 0 Then
Exit Sub
End If
' Get the selected item
Dim oMaterialName As String
oMaterialName = oComboBoxDef.ListItem(oComboBoxDef.ListIndex)
' Set a reference to the active part document
Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveDocument
Dim oPartCompDef As PartComponentDefinition
Set oPartCompDef = oDoc.ComponentDefinition
' Get the selected material
Dim oMaterial As Material
Set oMaterial = oDoc.Materials.Item(oMaterialName)
' Change the part material
oPartCompDef.Material = oMaterial
End Sub