Board index » delphi » Activedocument in Word

Activedocument in Word


2004-01-23 09:01:48 PM
delphi246
Hi all,
I have an application that uses Word to create and modify documents.
There is a problem however with the wordapplication.activedocument-property,
in the following case:
-a first document is being opened,
-another, second document is being opened,
-using the taskbar we switch back to the first document,
If I try to access wordapplication.activedocument in that case, I get the
reference to the second document (which is not the active one at that time).
Has anyone experienced this and knows how to solve or work-around this,
because this is a very annoying problem ?
Any help appreciated,
Glenn
PS Using the de{*word*81} (setting a breakpoint for instance) is no option,
because in that case you will get the correct document.
 
 

Re:Activedocument in Word

Quote
I have an application that uses Word to create and modify documents.
There is a problem however with the
wordapplication.activedocument-property,
in the following case:
-a first document is being opened,
At this point you should save the reference to the doucment, eg
FirstDoc:=WordApp.ActiveDocument;
Quote
-another, second document is being opened,
-using the taskbar we switch back to the first document,

If I try to access wordapplication.activedocument in that case, I get the
reference to the second document (which is not the active one at that
time).

Has anyone experienced this and knows how to solve or work-around this,
because this is a very annoying problem ?
Well you can find any document with code like Documents("Document1").Item
which will find the relevant document with that name. Programming with
ActiveDocument is always dangerous, as you never quite know what the user or
word will do.
Oliver Townshend
 

Re:Activedocument in Word

Oliver,
that is the problem. I need to know in what document the user is working,
not the other way around !
The only way I know of to get the document the user is working on, is the
activedocument-property.
In the case described below however, it returns a wrong value
(documentvariable): the second document that has been opened instead of the
document the user is really working on.
I am aware of the documents("doc").item-option, but it will not solve my
problem here.
Regards
Glenn
"Oliver Townshend" <XXXX@XXXXX.COM>writes
Quote
>I have an application that uses Word to create and modify documents.
>There is a problem however with the
wordapplication.activedocument-property,
>in the following case:
>-a first document is being opened,

At this point you should save the reference to the doucment, eg
FirstDoc:=WordApp.ActiveDocument;

>-another, second document is being opened,
>-using the taskbar we switch back to the first document,
>
>If I try to access wordapplication.activedocument in that case, I get
the
>reference to the second document (which is not the active one at that
time).
>
>Has anyone experienced this and knows how to solve or work-around this,
>because this is a very annoying problem ?

Well you can find any document with code like Documents("Document1").Item
which will find the relevant document with that name. Programming with
ActiveDocument is always dangerous, as you never quite know what the user
or
word will do.

Oliver Townshend