VBA can't use component occurrence as argument of a function

VBA can't use component occurrence as argument of a function

Anonymous
Not applicable
1,118 Views
7 Replies
Message 1 of 8

VBA can't use component occurrence as argument of a function

Anonymous
Not applicable

Hello,

 I am trying to loop trough an assembly with multiple subassemblies and parts, and I can't manage to use the componentoccurence as an argument of the recursive function LoopAssembly (oPart)

I always get:

run-time error '438'
Object dosen't support this property or method

 

I have also tried this by looping trough each Bomrow of a BOM, and i had the same issue when passing the Bomrow as an argument.

 

I would appreciate some help in this issue.

 

Sub InspectAssembly()

Dim oApp As AssemblyDocument
Set oApp = ThisApplication.ActiveDocument

Dim oPart As ComponentOccurrence

 

For Each oPart In oApp.ComponentDefinition.Occurrences
    MsgBox oPart.Name
    If oPart.SubOccurrences.Count > 0 Then LoopAssembly (oPart)
Next
End Sub


Function LoopAssembly (Asse As ComponentOccurrence)

Dim oPartfunc As ComponentOccurrence

For Each oPartfunc In Asse.SubOccurrences
     MessageBox.Show (oPartfunc.Name)
If oPartfunc.SubOccurrences.Count > 0 Then LoopAssembly (oPartfunc)
Next

End Function

 

Thank you

0 Likes
Accepted solutions (1)
1,119 Views
7 Replies
Replies (7)
Message 2 of 8

bradeneuropeArthur
Mentor
Mentor
For Each oPart as object In oApp.ComponentDefinition.Occurrences
    MsgBox oPart.Name
    If oPart.SubOccurrences.Count > 0 Then LoopAssembly (oPart)
Next
End Sub

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 8

dgreatice
Collaborator
Collaborator
Accepted solution

You forgot"End If".

 

Sub InspectAssembly()

Dim oApp As AssemblyDocument
Set oApp = ThisApplication.ActiveDocument

Dim oPart As ComponentOccurrence

 

For Each oPart In oApp.ComponentDefinition.Occurrences
MsgBox oPart.Name
If oPart.SubOccurrences.Count > 0 Then

Call LoopAssembly (oPart)

End If
Next
End Sub


Function LoopAssembly (Asse As ComponentOccurrence)

Dim oPartfunc As ComponentOccurrence

For Each oPartfunc In Asse.SubOccurrences
MessageBox.Show (oPartfunc.Name)
If oPartfunc.SubOccurrences.Count > 0 Then

Call LoopAssembly (oPartfunc)

End if
Next

End Function

Please use the ACCEPT AS SOLUTION or KUDOS button if my Idea helped you to solve the problem.

Autodesk Inventor Professional Certified 2014
0 Likes
Message 4 of 8

Anonymous
Not applicable

Hello,

 

It doesn't work. I had to change the sintax as "For Each oPart as object" doesn't work on VBA, and used Dim oPart as Object instead of component occurence,. But the result was the same, unable to pass oPart as an argument.

 

Thank you

0 Likes
Message 5 of 8

Anonymous
Not applicable

Hello,

 

That is not the problem. Actually you don't need to use end if, if you are just writting one line of code ahead of then.

 

Thank you anyway for the attention

0 Likes
Message 6 of 8

dgreatice
Collaborator
Collaborator

Did you see my red highlight?

 

When I try you code, same error are show.

Please use the ACCEPT AS SOLUTION or KUDOS button if my Idea helped you to solve the problem.

Autodesk Inventor Professional Certified 2014
0 Likes
Message 7 of 8

bradeneuropeArthur
Mentor
Mentor
Public Sub main()

Dim a As Application
Set a = ThisApplication

Dim b As AssemblyDocument
Set b = a.ActiveDocument

Call InspectAssembly(b)
End Sub
Sub InspectAssembly(oApp As AssemblyDocument)

Dim oPart As ComponentOccurrence
Dim aascom As AssemblyComponentDefinition
Dim partComp As PartComponentDefinition

Set aascom = oApp.ComponentDefinition
For Each oPart In aascom.Occurrences
    MsgBox oPart.Name
    
    If oPart.DefinitionDocumentType = kAssemblyDocumentObject Then
    'LoopAssembly (oPart.)
    InspectAssembly (oPart.Definition.Document)
    End If
    'If oPart.SubOccurrences.Count > 0 Then
    
    
    'End If
Next
End Sub

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 8 of 8

Anonymous
Not applicable

Hello,

Thank you for the tip. Using Call solved my problem.

I was trying to understand your end if reference, that my eyes didn't see  the "Call" word 

 

This works like a charm now.

Sub InspectAssembly()

Dim oApp As AssemblyDocument
Set oApp = ThisApplication.ActiveDocument
Dim oPart As ComponentOccurrence

For Each oPart In oApp.ComponentDefinition.Occurrences
MsgBox oPart.Name
If oPart.SubOccurrences.Count > 0 Then Call LoopAssembly(oPart)
Next

End Sub


Function LoopAssembly(Asse As ComponentOccurrence)

Dim oPartfunc As Inventor.ComponentOccurrence
For Each oPartfunc In Asse.SubOccurrences
MsgBox (oPartfunc.Name)
If oPartfunc.SubOccurrences.Count > 0 Then Call LoopAssembly(oPartfunc)
Next

End Function

 

0 Likes