Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
WCrihfield
in reply to: utm007

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

EESignature

(Not an Autodesk Employee)