Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

ms office - Word VBA and Multiple Word Instances

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...