im changing material using vba but if the material doesnt exist it creates error
can someone help me ??
Solved! Go to Solution.
Public Sub Change_Material() Dim oPartDoc As PartDocument Set oPartDoc = ThisApplication.ActiveDocument Dim oPartCompDef As PartComponentDefinition Set oPartCompDef = oPartDoc.ComponentDefinition Dim oMaterial As material On Error Resume Next Set oMaterial = oPartDoc.Materials.Item("En8") If Err.Number <> 0 Then Set oMaterial = oPartDoc.Materials.Add("En8", 7.8) Err.Clear End If oPartCompDef.material = oMaterial End Sub
Try This, to add more details to the new material see programming help "Create Material"
this is good if i know the material im looking for.
what if i have a bunch of ipts from somewhere with materials that are not in our library.
i want to click a button to match that outsider material in our library.
i managed to match other materials to our library as long as its listed in our library
but if it doesnt exist in our library the program stop.
id like to add a code that will catch the error and message "material doesnt exist"
and add that particular material to our library.
therefore i cant name that outsider material in the code.
is this possible???
Public Sub Material_Add() Dim oPartDoc As PartDocument Set oPartDoc = ThisApplication.ActiveDocument Dim oPartCompDef As PartComponentDefinition Set oPartCompDef = oPartDoc.ComponentDefinition Dim oMaterial As material Set oMaterial = oPartCompDef.material Dim strMaterial As String strMaterial = oMaterial.Name On Error Resume Next oMaterial.SaveToGlobal If Err.Number <> 0 Then MsgBox ("Material " & strMaterial & "already exists in Library") Err.Clear Exit Sub Else MsgBox ("Material " & strMaterial & "Added to Library") End If End Sub
Hope this is what you are looking for.