File Open not active document

J

JRob

I am in the process of migrating from Word 2002 to Word 2007 and have
several macros developed in the later.

One macro opens files using the Documents.Open(strFileName)

Previously the file opened would become the activedocument in the Word
Application window. However in Word 2007 the file opened is not the
activedocument and it has to be selected from the task bar or by using
Alt + Tab.

I have tried various workarounds to sort this (SendKeys etc) to no
avail. Any ideas would be welcome.
 
J

Jay Freedman

JRob said:
I am in the process of migrating from Word 2002 to Word 2007 and have
several macros developed in the later.

One macro opens files using the Documents.Open(strFileName)

Previously the file opened would become the activedocument in the Word
Application window. However in Word 2007 the file opened is not the
activedocument and it has to be selected from the task bar or by using
Alt + Tab.

I have tried various workarounds to sort this (SendKeys etc) to no
avail. Any ideas would be welcome.

The most recently opened document should become the ActiveDocument, and I'm
not sure what would interfere with that. Unfortunately I'm away from my copy
of Word 2007 right now, so I can't test it to see if it behaves the same as
yours.

In any case, you can force it this way:

Dim wdDoc As Document
Set wdDoc = Documents.Open(strFileName)
wdDoc.Activate

If the document is already active, calling Activate on it won't do anything.

Technically, you can do this in one statement,

Documents.Open(strFileName).Activate

but it's usually a good idea -- especially when there are two or more
documents open at the same time -- to use an assigned Document object like
wdDoc to do all operations in the macro instead of using the ActiveDocument
object. Then you always know which document is going to be the target of the
operation. In fact, the document doesn't even have to be active to do things
like formatting, data extraction, Find/Replace, etc.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
T

Tony Jollans

I'll second Jay on that. Do you have any AddIns (or code in the document or
its template) that might be doing something to change the ActiveDocument?
What do you see as you step through the code?
 
J

JRob

Thank you both for your replies. I will not be back in the office
until Monday so will try your suggestions then.
 
J

JRob

The file selection to open was displayed in a dialog box.
Interestingly

doc.Activate
Unload Me (unloading the dialog box)
does not make doc the active document

However:
Unload Me
doc.Activate
works perfectly
 
R

Russ

JRob,
The file selection to open was displayed in a dialog box.
Interestingly

doc.Activate
Unload Me (unloading the dialog box)
does not make doc the active document
The above probably did not work because you opened the dialog in the default
modal (versus modeless) way, which meant that nothing could be selected
(activated) while the form (dialog) was still being shown.
 

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