05-26-2022
07:07 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
05-26-2022
07:07 AM
Hi @utm007. Try this version of your code. This is in VBA, so it would be a VBA macro, and it would go into a regular 'Module'. I'm just using that material name you were trying to use in your first post, so if that material does not exist, it will let you know, then exit the sub routine before trying to set it to the part.
Sub ChangeMaterial()
If ThisApplication.ActiveDocumentType <> kPartDocumentObject Then
Call MsgBox("This VBA macro only works on Parts.", vbCritical, "Wrong Document Type")
Exit Sub
End If
Dim pDoc As PartDocument
Set pDoc = ThisApplication.ActiveDocument
Dim oMaterialDisplayName As String
oMaterialDisplayName = "TestMaterial"
Dim oMyMaterial As MaterialAsset
Dim oMaterial As MaterialAsset
For Each oMaterial In pDoc.MaterialAssets
If oMaterial.DisplayName = oMaterialDisplayName Then
oMyMaterial = oMaterial
End If
Next
If oMyMaterial Is Nothing Then
For Each oMaterial In ThisApplication.ActiveMaterialLibrary
If oMaterial.DisplayName = oMaterialDisplayName Then
oMyMaterial = oMaterial.CopyTo(pDoc)
End If
Next
End If
If oMyMaterial Is Nothing Then
Call MsgBox("The specified material was not found.", vbCritical, "")
Exit Sub
End If
pDoc.ActiveMatrial = oMaterial
End Sub
If you need the iLogic version, you can pretty much just get rid of the 'Set' keywords, and the 'Call' keywords, and it will probably work just fine there.
Wesley Crihfield
(Not an Autodesk Employee)