Using ActiveDocument when mutliple documents open

W

Webboex

In Word 2007, if there are multiple documents open, ActiveDocument does not
always refer to the last document I viewed or typed in and instead sometimes
reverts back to the last document on which I used the procedure.

As I can replicate this problem with a one line macro
MsgBox ActiveDocument.Name
the problem is not related to the complexity of the code I am trying to run.

Is there any other way of referring to the document I last worked on or
viewed?
 
P

Pesach Shelnitz

Hi,

You can create a Document object representing any of the open documents from
the Documents collection using the file name of the document as follows.

Dim doc As Document
Set doc = Documents("MyFile.docx")

When you start running a macro, the ActiveDocument is the document from
which you launched the macro. Thus, you can create a Document object for the
ActiveDocument and use this object to refer to the original active document
later on as follows.

Dim doc As Document
Set doc = ActiveDocument

When you open a file, you can also create a Document object to represent it.

Dim doc As Document
Set doc = Documents.Open(fileName)

You can also do this when you create a new file.
 
W

Webboex

Thanks for your response.

The problem is that Word does not always seem to recognise which document is
current when the macro is launched. The macro that I'm having problems with
is designed to print the active document but it often prints another document
that is open - not the active one. The problem is not in the macro itself as
I can replicate it with the one line MsgBox ActiveDocument.Name. I wonder
whether it is something to do with how Word 2007 handles document windows
(could it be a bug?)
 
T

Tony Jollans

I would expect Active Document to be as you say, and have never heard of
problems with it, but, and I am guessing a little, if you have "Windows in
Taskbar" set, there may be actions (Show Desktop, perhaps) that effectively
re-arrange the Windows so that what you thought was active is no longer
deemed so by the system.
 
W

Webboex

Even the simple macro:

Sub TestActiveDocument()
MsgBox ActiveDocument.Name
End Sub

cannot reliably identify the active document every time. I can only think it
has something to do with how Word 2007 handles its document windows. I have
never had a problem with it in earlier versions of Word.
 
T

Tony Jollans

Thousands of people rely on ActiveDocument every day; if there is a problem
it needs identifying. You are the only person, to my knowledge, seeing such
a problem at the moment - perhaps it's SP2 or some other recent update
causing it.

You seem to be coming across this often enough for it to be a problem so
could you please tell us under precisely what conditions you see the problem
and what actions (both inside and outside Word) precede its occurrence.
Please include, at a minimum, the version of Word 2007 and updates applied,
the operating system and version, other applications (at least all
VBA-enabled ones) running at the time, the state of the "Windows in Taskbar"
option, and any other options you think relevant.
 
W

Webboex

Tony, I don't understand the "Windows in Taskbar" but since I can replicate
the problem with a one line macro I'm not sure it is relevant. For
information, the problem occurs in Word 2007 with and without service pack 2
and on Windows XP and Vista. The problem originated with a print macro that
prompts for a printer and the paper trays to use, drops a header and footer
into the active document, prints the active document and then opens the
SaveAs dialog box. If the user has another document open at the time, it is
sometimes this document, and not the active one that is printed. I would
suspect the code that in this macro if it wasn't for the fact that my one
line macro also sometimes incorrectly identifies the active document.
 
T

Tony Jollans

Windows in Taskbar determines whether each document is shown as a separate
window in the taskbar (so you can switch between them using Alt+Tab
(Winkey+Tab, etc.) as well as Ctrl+F6, or whether document windows all exist
within one parent Word Window (so you need to use Ctrl+F6 or mouse clicks to
switch). The significance is that windows which show in the Taskbar are
subject to operating system influence, which may affect which window, and
hence which document, is considered active, and which is why I ask precisely
what actions precede the error.
 
W

Webboex

Tony,

Yes, Windows in Taskbar is set - I will see if the same problem occurs
without it.

Thanks for your help.
 

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