Set active window in Word with VBA

S

sait

Have a worddoc with vba-kode. sub AutoOpen does some mailmerging and sends
the resulting merged doc to a new doc with the wdSendToNewDocument. The new
doc is saved without the macro, and works fine. The vba-code close the
template, and remains with the new doc, wich is also saved . The problem is
that if there are previously open doc's in word, I loose focus from the new
saved doc when the macro finishes. Focus is set by word to the first window
in the active windows list. This means that the user will have to navigate
windows to make the new doc visible. I want the macro to handle this, and
have tried several techniques to activate the new window, wich may be number
2 og more in the list, depending on the number of open docs. I'm not able to
overload this. I was hoping that Documents(docName).Activate or
ActiveDocument.Activate would work, but it seems like this code is run and
then word runs it's own code afterwords. Some one who knows? Thanks! (Tried
many other techs too)
 
P

Perry

I never use Documents(x) or Windows(y) to allocate the correct document.

Advise:
use object variables indicating which document y're targetting.
Like examplified below

Dim doc As word.document
Set doc = word.documents.add

Now [doc] is the pointer to the newly created document, and regardless how
many windows/documents are opened,
following statement will affect the correct document
doc.Activate

Even without activating the correct document, below statement will change
the text
of the first row, second column of the correct document.
doc.Tables(1).Cell(1, 2).Range = "some text"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top