Activating Documents vs. Windows

G

Greg Oij

I've been looking at the MVP website trying to decipher
the difference in VBA between:

Dim dDOC as Document

dDoc = Documents.Add
Documents(dDoc).Activate

AND

Dim iWinNumb as Integer

dDoc = Documents.Add
iWinNumb = ActiveWindow.Index

Windows(iWinNumb).Activate

Does anyone know which is more reliable for activating a
given Window or Document in Word's VBA? (esp. Word 2003)
 
J

Jezebel

The first is better: it simply activates the document, which will always
succeed if the document is open.

The second relies on the fact that adding a document automatically activates
it, so checks the index of the window containing it, then activates the
document by calling that window. This is superfluous if the action is
carried out immediately. And it won't necessarily work if there are
intervening instructions (between setting iWinNumb and activating that
window) that might open or close other documents. The window index for a
given document is the document's position on the Window menu: these
positions change as documents are opened and closed or new windows opened on
existing documents.
 
J

Jay Freedman

Just to be nit-picky about syntax (because VBA certainly will be), the
first form should be written as

Dim dDOC as Document

Set dDoc = Documents.Add
dDoc.Activate
 
G

Greg Oij

Jezebel: Thanks for replying. That is what I suspected.

Now if I could just get Win XP / Word 2003 to stop
occasionally "wander to the other document" and reliably
display the activated document on top. Same code works
100% of the time on previous releases of Word.
 

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