Hi @e_frissell. I knew your post seemed familiar somehow but couldn't recall why. I had used some code pretty much identical to the one you first posted years ago when I was first getting started into this stuff, but my code was in iLogic instead of VBA. At the time I used a simple iLogic rule to investigate what those internal names were in other document types, because when I first started responding on these forums, I found out that there were folks on here from many other countries, and that the 'English' names for some stuff I was working with did not always work some of those other folks who had other language versions of Inventor. Those internal names were stable across all languages, unlike the 'local' regular names of some objects. I did not use a Function like the nice one posted above by @A.Acheson, but came to similar conclusion. I had two different versions of the iLogic code. One version looped through all top level nodes under the top node, and made sure they were not expanded. The other version selected that top node, then executed a command, similar to the one that gets executed when you use the right-click method manually. Below are both versions of code, but in VBA instead of iLogic, since your original code above was in VBA.
Version 1:
Sub CollapseModelTreeNodes()
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument
Dim oTopNode As BrowserNode
If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
Set oTopNode = oDoc.BrowserPanes.Item("AmBrowserArrangement").TopNode
ElseIf oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
Set oTopNode = oDoc.BrowserPanes.Item("DlHierarchy").TopNode
ElseIf oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
Set oTopNode = oDoc.BrowserPanes.Item("PmDefault").TopNode
End If
If oTopNode Is Nothing Then Exit Sub
'If oTopNode.Expanded Then oTopNode.Expanded = False 'can not undo manually
If oTopNode.BrowserNodes.Count > 0 Then
Dim oNode As BrowserNode
For Each oNode In oTopNode.BrowserNodes
If oNode.Visible And oNode.Expanded Then oNode.Expanded = False
Next
End If
End Sub
Version 2:
Sub CollapseModelTreeNodes2()
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument
Dim oTopNode As BrowserNode
If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
Set oTopNode = oDoc.BrowserPanes.Item("AmBrowserArrangement").TopNode
ElseIf oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
Set oTopNode = oDoc.BrowserPanes.Item("DlHierarchy").TopNode
ElseIf oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
Set oTopNode = oDoc.BrowserPanes.Item("PmDefault").TopNode
End If
If oTopNode Is Nothing Then Exit Sub
oDoc.SelectSet.Clear
oDoc.SelectSet.Select (oTopNode.NativeObject)
ThisApplication.CommandManager.ControlDefinitions.Item("AppBrowserCollapseChildrenCmd").Execute
End Sub
Since a similar amount of code is used for both versions, I greatly prefer the version that does not call the command to execute though. But since my installation of Inventor is for English language, I tend to simply use the name "Model" in my working code, to keep the code shorter and simpler, and that works just fine for me, without needing to know the internal names. Plus, in my iLogic versions, I tend to use 'ThisDoc.Document' instead of 'ThisApplication.ActiveDocument', because it tends to work better in more scenarios...just a personal preference. I have also been avoiding the use of VBA as much as possible, in favor of iLogic the past couple years, for security reasons. Below is a link to an article about that.
https://www.autodesk.com/support/technical/article/caas/sfdcarticles/sfdcarticles/Microsoft-Visual-B...
Wesley Crihfield
(Not an Autodesk Employee)