0

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
  • [This answer](https://stackoverflow.com/a/13634579/4717755) discusses that you may have to attach to a running instance of Word and then kill that instance when you're done. It's at least a step in the direction you're headed. – PeterT Oct 21 '21 at 15:47
  • This one might help as well: https://stackoverflow.com/a/59998068/16578424 - it looks for _all_ open windows via but you can filter for e.g. "- Word" etc. – Ike Oct 21 '21 at 16:18
  • Well fellas, thanks so much for those replies. They lead me to the exact information I needed. Basically, impossible to do without closing things. Still, dang clever! I haven't taken the time to get it worked out but, basically, iterate through the open windows identifying those that are word documents to load the listbox with the names. User selects the document requested and VBA iterates through open documents looking for a match and closing whatever doesn't match until it comes across the right one. Could work in a save and open afterwards. Quite clever. Thanks! – Guynoeyes Oct 22 '21 at 00:48

0 Answers0