Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

code for catching error if the material doesnt exist

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
Anonymous
454 Views, 5 Replies

code for catching error if the material doesnt exist

im changing material using vba but if the material doesnt exist  it creates error

 

can someone help me ?? Smiley Frustrated

5 REPLIES 5
Message 2 of 6
Anonymous
in reply to: Anonymous

helloooooSmiley Sad

Message 3 of 6
Anonymous
in reply to: Anonymous

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"

Message 4 of 6
Anonymous
in reply to: Anonymous

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???

Message 5 of 6
Anonymous
in reply to: Anonymous

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.

Message 6 of 6
Anonymous
in reply to: Anonymous

thanks a lot sas!!Smiley Happy

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report