I'm using VBA to pull data from Word documents into Excel (Office 2016). User clicks a button in an Excel template and a UserForm pops up listing all of the open Documents in a listbox. I'm having trouble with not finding all open documents as they aren't all under the same Word application. Not sure if that makes much sense but let's say I have 2 Word documents open, Word1 and Word2. In my Excel template, I click the button and should see Word1 and Word2 in the listbox but I only see Word1; this tends to happen when I highlight multiple Word documents and open them all at once though there are other causes. I go to Word1 or Word2 and press Alt+F11 and I can see that each document is alone in the VBE menu (along with the Normal addin thing). I can close Word2 and reopen it and it will usually reopen under the same Word application as Word1. If that doesn't work then I can open Word2 from Word1 which forces it to open under the same Word application as Word1. I can also close Word1 and click the button again and then it will pick up Word2. I'd like to just list all open Word applications despite whatever particular Word application each Document happens to be running under. I can iterate through all open Word documents but, if there are multiple Word applications open, it just iterates through one set. Can I iterate through all open Word applications? I'm using the following code to populate the listbox when the UserForm opens:
Dim wdApp As Object, mObj As Object
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
Set mObj = wdApp.Document
On Error GoTo 0
If wdApp Is Nothing Or IsEmpty(wdApp) = True Then
MsgBox "There doesn't appear to be any Word documents open. If there are Word Documents open, save them (if required) and close them all, then open only the one document that has the required information."
Call cClear
Exit Sub
End If
For Each mObj In wdApp.Documents
If Len(mObj.Name) > 37 Then
UserForm1.ListBox1.AddItem Left(mObj.Name, 37) & "..."
Else
UserForm1.ListBox1.AddItem mObj.Name
End If
Next mObj