Inventor VBA - Get Derived Parts In Assembly

Inventor VBA - Get Derived Parts In Assembly

isocam
Collaborator Collaborator
1,063 Views
2 Replies
Message 1 of 3

Inventor VBA - Get Derived Parts In Assembly

isocam
Collaborator
Collaborator

Can anybody help?

 

I have the following macro. I want to list all "derived parts" in a Assembly document.

 

Public Function GetDerivedParts()
    Dim oApp As Application

    Dim oPD As AssemblyDocument

    Dim oPCD As PartComponentDefinition

    Dim oDerPart As DerivedPartComponent

    Set oApp = ThisApplication

    Set oPD = oApp.ActiveDocument

    Set oPCD = oPD.ComponentDefinition

    For Each oDerPart In oPCD.ReferenceComponents.DerivedPartComponents
        MsgBox (oDerPart.Name)
    Next
End Function

 

I am getting the error "13 - Type Mismatch".

 

Does anybody know what I am doing wrong?

 

Many thanks in advance!

 

Darren

0 Likes
Accepted solutions (1)
1,064 Views
2 Replies
Replies (2)
Message 2 of 3

JhoelForshav
Mentor
Mentor

Hi @isocam 

You are setting an AssemblyComponentDefinition to a PartComponentDefinition.

Try this 🙂

 

Sub GetDerivedParts()
	Dim oApp As Application

	Dim oPD As AssemblyDocument

	Dim oPCD As AssemblyComponentDefinition

	Dim oDerPart As DerivedPartComponent

	Set oApp = ThisApplication

	Set oPD = oApp.ActiveDocument

			For Each oRefDoc As Document In oPD.ReferencedDocuments
				If oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject
					For Each oDerPart In oRefDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents
						MsgBox(oDerPart.Name)
					Next
				End If
			Next
End Sub
0 Likes
Message 3 of 3

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @isocam 

Sorry, I didn't try the first code i posted as a VBA macro. (Tried to just translate it from VB.NET without compiling it)

This works!

 

Sub GetDerivedParts()
    Dim oApp As Application

    Dim oPD As AssemblyDocument

    Dim oDerPart As DerivedPartComponent
    
    Dim oRefDoc As Document
    
    Set oApp = ThisApplication

    Set oPD = oApp.ActiveDocument

            For Each oRefDoc In oPD.ReferencedDocuments
                If oRefDoc.DocumentType = kPartDocumentObject Then
                    For Each oDerPart In oRefDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents
                        MsgBox (oDerPart.Name)
                    Next
                End If
            Next
End Sub