VBA list open documents

VBA list open documents

pball
Mentor Mentor
6,690 Views
8 Replies
Message 1 of 9

VBA list open documents

pball
Mentor
Mentor

Is there a way to get a list of the currently open documents in VBA?

 

Right now to process a bunch of open drawings I simply process the active drawing and then close it, then repeat until all are closed. I'd like to be able to get a list of open documents and switch between them to process them all, which would leave them open.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Accepted solutions (1)
6,691 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable

Private Sub UserForm_Initialize() 
  Dim oDoc As Inventor.Document


  For Each oDoc In ThisApplication.Documents
    ListBox1.AddItem oDoc.FullFileName
  Next oDoc
End Sub

 

Private Sub ListBox1_Click()
  Dim sFile As String
  Dim index As Integer
  index = ListBox1.ListIndex

  If index < 0 Then Exit Sub

 

  sFile = ListBox1.List(index)
  ThisApplication.Documents.ItemByName(sFile).Activate
End Sub

 

 

0 Likes
Message 3 of 9

pball
Mentor
Mentor

Thanks, I'll have to give it a shot if I ever get some free time at work lol.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 4 of 9

robmatthews
Collaborator
Collaborator

Yes, that's what i do (thisapplication.Documents), but when you have assemblies open, it counts each component as well.

 

That is, I have one assembly document "open", but the .count of ThisApplication.Documents returns 97.

=============================================
This is my signature, not part of my post.
0 Likes
Message 5 of 9

pball
Mentor
Mentor

Ok this doesn't seem like it will work then. I just want to list each open assembly, part, and drawing. I just did debug.print ThisApplication.Documents.Count with an assembly and then an assembly drawing and they both gave over 90 which is the amount of parts and stuff in them.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 6 of 9

Anonymous
Not applicable

Inventor loads only what it needs to for the current documents to build.
If you load an assembly drawing Inventor will load all components in a super lightweight mode for reference.
It doesn't list those under ThisApplication.documents.

To fully load all compents you need to start at the top level document and use its AllReferencedDocuments Property
which returns an enumerated list of all components it takes to create that top level document.
You would gather The fullfilename property of each component and add them to the list.

If you have many files loaded with many nested components then you will have to
use the AllReferencedDocuments Property for each and check if each component

fullfilename is in the listbox. if the component filename does not exists in the listbox add it.

When clicking in the listbox you would probably want to use Documents.Open() instead of Documents.Itembyname()
As the latter will fail if the referenced component is not already fully loaded.

 

Be aware though that fully loading all components like this is memory intensive and you can run out of resources

depending on your system and the size of the components.

0 Likes
Message 7 of 9

pball
Mentor
Mentor

I think you might be misunderstanding what I'm trying to do. As I'm not trying to do anything with components of an assembly.

 

Attached is a picture showing a few different parts, assemblies, and drawing files open at the same time. I want to make a list of those, not the components of any of them.

 

So if I have those files open, I'd like a list like the following.

 

plate + rod.iam

longbar.idw

bent tube with hole.ipt

rod.ipt

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 8 of 9

Anonymous
Not applicable

If that is the case then I fail to see why you would not use the code from my original post.

 

The only change would be that we write a small function to strip off the fullfilename path information before we

add it to the listbox.

 

If you still want to make the selected document the active document then you would need

to store the fullfilename in an array or collection so that you could use that with ItemByName()

 

-------

 

Public Function GetFileName(ByVal FullFileName) As String
  Dim nPos As Integer
  Dim sName As String

  sName = FullFileName
  nPos = InStrRev(sName, "\")
  If nPos > 0 Then
    sName = Mid(sName, nPos + 1)
  End If
  GetFileName = sName
End Function

0 Likes
Message 9 of 9

pball
Mentor
Mentor
Accepted solution

Gruff your original post isn't what I want.

 

I just did a test and it loads all the compents of the open file an assembly in this case. I just want the main file that is open added to the list.

 

I do not want a list of the components contained in an open file, I just want the names of the open files.

 

 

Attached is a picture that shows the list populated by your code. plate + rod.iam is the only file open and the only file I want to list, not any subcomponents.

 

-----------------------

Update.

 

I did find a way to do what I want.

 

  For Each oDoc In ThisApplication.Documents.VisibleDocuments
    ListBox1.AddItem filename(oDoc.FullFileName)
  Next oDoc

 

Adding the .VisibleDocuments so far appears to just reference open documents and not subcomponents.

 

Added a second picture to the difference between your code and my code.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style