05-26-2022
08:06 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
05-26-2022
08:06 AM
Here is the corrected full code again, since I made so many hasty mistakes the first time. This time I created a material by that name, then tested it out and fixed the code properly so that it worked in my tests.
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
Set oMyMaterial = oMaterial
End If
Next
If oMyMaterial Is Nothing Then
For Each oMaterial In ThisApplication.ActiveMaterialLibrary.MaterialAssets
If oMaterial.DisplayName = oMaterialDisplayName Then
Set 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.ActiveMaterial = oMyMaterial
End Sub
Wesley Crihfield
(Not an Autodesk Employee)