2

Good morning.

I am having a problem with getting my code to find other instances of word and have hit a brick wall after much google searching.

My Code below will find all open word documents and populate them into a combo box.

My problem is we have applications (I have no control over these) that will open word documents in a new instance and therefore my code will not find/control these documents.

Any ideas?

Dim objWordDocument As Word.Document
Dim objWordApplication As Word.Application


'//find all open word documents
Set objWordApplication = GetObject(, "Word.Application")

'//clear combobox
    OpenDocs.Clear

'//add all open documents to combo box

        For Each objWordDocument In objWordApplication.Documents

            OpenDocs.AddItem objWordDocument.Name

        Next objWordDocument
Berkay Turancı
  • 3,373
  • 4
  • 32
  • 45
Mikey
  • 107
  • 1
  • 3
  • 8
  • My answer below let you iterate through all Word instances, including zumbies, not having to kill any; from this point you will get any instance with Get `AccessibleObjectFromWindow` – Marcelo Scofano Diniz Nov 28 '20 at 13:05

2 Answers2

3

From what I have seen, and come to understand, the only sure fire way to do this is to iterate through the running instances of word and then kill each one in turn to be sure that you are getting the next instance.

Since word registers in the running object table the same exact way for every instance of itself, there is no way to get through them without first closing the one you were looking at.

One option to this approach, which is probably not desirable, is to get all the file names while you are killing the application instances and then load them all back in one instance that you create.

Alternately if you know the names of the open files, you can 'getObject' by open file name since Word will push its document names into the running object table, sadly this does not sound like the case for you.

Without writing an active x MFC service, you are not going to be able to do what you are looking to do.

I hope that is helpful.

EDIT:

there was an extensive discussion on subclassing and windows API's to get handles in order to change focus. http://www.xtremevbtalk.com/showthread.php?t=314637

if you dove into that head first and were able to enumerate the word instances by hwnd then you could potentially focus each one in turn and then list the file names. I do warn you though; that is some nasty subclassing which is dark magic that only some people who really want to accidentally break stuff play with.

In any event if you wanted to take the look at one instance, kill, repeat, reopen try this:

Adapted from this thread: http://www.xtremevbtalk.com/showthread.php?t=316776

Set objWordApplication = GetObject(, "Word.Application")

'//clear combobox
    OpenDocs.Clear

'//add all open documents to combo box

   Do While Not objWordDocument is nothing 

        For Each objWordDocument In objWordApplication.Documents

            OpenDocs.AddItem objWordDocument.Name

        Next objWordDocument
        objWordApplication.Quit False
        Set objWordApplication = Nothing
        Set objWordApplication = GetObject(, "Word.Application")
   loop

   ** use create object to open a new instance of word here and then go though
   ** your list of files until you have opened them all as documents in the new
   ** instance.
Pow-Ian
  • 3,607
  • 1
  • 22
  • 31
  • Thanks for your response @Pow-Ian. You are correct and I will not know the names of the open documents in the second instance. I will look into killing my code/application and have it reopen in the seocnd instance and see if that works. Any tips on doing that? – Mikey Nov 29 '12 at 21:22
  • Well you could kill it and get the file name easily enough, however bringing the the same instance back to life so that the application/process that created it would recognize it is way outside the bounds of what is possible in vba, unless there is a way to subclass it. I will elaborate on grabbing and killing. – Pow-Ian Nov 29 '12 at 21:30
  • Many thanks for your help so far! I will mark your post as useful once I have 15 rep :) – Mikey Nov 29 '12 at 21:45
  • no problem. If you do decide to call the windows API and start subclassing, I promise I won't tell you I told you so when something goes horribly wrong. – Pow-Ian Nov 29 '12 at 21:47
  • I will have a little play with it this afternoon and see how I get on. Thanks for your help and advice :) – Mikey Nov 29 '12 at 22:36
  • @Pow-Ian, you were right about windows API approach as better for this situation. I managed to get it for any Office App; see my answer below that redirects to the complete investigation and answer. also, I believe that there is a mistype in your code, indicated there. – Marcelo Scofano Diniz Nov 28 '20 at 12:37
0

It's an old thread, but I too have the need to iterate over Word instances and bumped here.

Following the @Pow-Ian's advise, I tried to do the

if you dove into that head first and were able to enumerate the word instances by hwnd then you could potentially focus each one in turn and then list the file names.

Although I have managed to get all the handles, I have found an easier strategy with regard to office applications through AccessibleObjectFromWindow and our question is now solved.

Also, I believe the code showed @Pow-lan's has a mistype on

Do While Not objWordDocument is nothing

and should be:

Do While Not objWordApplication is nothing
  • @Martijn Pieters, I slightly modified my answer to better conform formatting. Otherwise, I do not have any clue why you deleted my former answer that already had an up-vote... could you clarify it for me? As I understand, the answer here, that redirects to another will not fit in a comment and it is not illegal; and after I even point a mistype in another answer - ok, this could have been a comment. – Marcelo Scofano Diniz Nov 28 '20 at 12:53
  • As for the reasons for delete answers: 1) commentary on the question or other answers, 2) asking another, different question, 3) exact duplicates of other answers, 3) not even a partial answer to the actual question, I understand that 1) may apply to the last half, but the first half is were I'm proposing a different strategy that avoid him to kill and reopen all Word instances. I could copy and paste some code from there, but I think that as an encyclopedia is more useful to link answers... but if you or any moderator think so, I can delete this and post an actual complete answer... – Marcelo Scofano Diniz Nov 28 '20 at 13:00