How to adress the right document

B

Bo Hansson

I know that this matter has been discussed several times in the past, and I
thought that I had the solution - but now I must admit, I had not!
The problem is the following:
Having several documents open in Word, I want to operate on a certain one. I
use Set oDoc=ActiveDocument every time I start an operation and then, during
my processes, I use oDoc.Activate whenever I expect Word to fool me. But
it's not enough, I'm still getting fooled. It still happens that Word
suddenly have another opinion about what is the active document.

Please, is there a solid solution to this problem ?

/BosseH
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Bo,

There's no need to actually activate the document to have vba perform some
operations on it. If you are using Set oDoc = ActiveDocument, you will need
to make sure that the active document is actually the one to which you want
to set that reference. Once the reference is set however, you can just us
oDoc as the object on which you want the code to operate.

e.g. oDoc.Bookmarks("bookmarkname").Range.

or oDoc.Range

etc.

Show us the code that is causing you the problems and we can probably give
you more specific assistance.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 
B

Bo Hansson

Hi Doug

That's the way I try to use oDoc!

When starting, I manually select the desired one amongst all open documents.
Seeing my document on the screen, I then fire my code, e.g. from a menu
command. The first action taken in my code is to nail the document by Set
oDoc=ActiveDocument.

Having done that, one should believe that from now on its just to use oDoc
to reference the document. But - it is NOT.
Now and then (related to opening/closing of user forms ?) Word suddenly
prefer to activate one of the documents I have previously operated on.

For that reason, I have tried oDoc.Activate to restore the reference. Up to
now I have thought that this was the solution, but I now experience that
Word still can fool me.


/BosseH


"Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL ADDRESS"
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Bo,

See if there is anything in the article “Making a UserForm show above a
newly created Word 2000+
document” at:

http://word.mvps.org/FAQs/Userforms/KeepUserFmAboveDoc.htm

that is relevant to your problem.
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 
B

Bo Hansson

Hi Doug

Interesting article, and I suppose this could be the solution! But I cant
get my implementation of the code to work. I've tried the following:
______________________________________________________________
In my module of deklarations I've included:

Declare Function GetActiveWindow Lib "user32" () As Long

Declare Function SetParent Lib "user32" _
(ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

Public iHandleForm As Long
Public iHandleDoc As Long
Public iNull As Long
______________________________________________________________

I always start code execution from a Windows menu that includes a number of
commands. As the very first action when activating my menu I now have:

Set oDoc = ActiveDocument
iHandleDoc = GetActiveWindow()
_____________________________________________________________

Later in the process I open a user form, and its Activate event starts with:

iHandleForm = GetActiveWindow()
iNull = SetParent(iHandleForm, iHandleDoc)
oDoc.Activate
_____________________________________________________________

What happens is that Word freezes, it doesn't respond to any commands.

What's wrong ? Is the user form really the ActiveDocument at the beginning
of the Activate event ?

/BosseH


"Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL ADDRESS"
 
B

Bo Hansson

Sorry,

In my last sentence in previous mail, please read ActiveDocument as
ActiveWindow.


/BosseH
 
J

Jonathan West

Bo Hansson said:
Hi Doug

That's the way I try to use oDoc!

When starting, I manually select the desired one amongst all open documents.
Seeing my document on the screen, I then fire my code, e.g. from a menu
command. The first action taken in my code is to nail the document by Set
oDoc=ActiveDocument.

That is a good start, but you need to follow through by ensuring the
following.

1. That you do not use the ActiveDocument object anywhere within the
remainder of your macro. Wherever you have ActiveDocument, replace it with
oDoc.

2. That you do not use the Selection object anywhere within the rest of your
macro. Wherever you use the Selection object, use a Range variable instead.
Having done that, one should believe that from now on its just to use oDoc
to reference the document. But - it is NOT.
Now and then (related to opening/closing of user forms ?) Word suddenly
prefer to activate one of the documents I have previously operated on.

OK, be carwfeul here. if you have done a set oDoc = ActiveDocument, then
code referring to oDoc will still operate on the original document even if a
different document is activated while the macro is running.
For that reason, I have tried oDoc.Activate to restore the reference. Up to
now I have thought that this was the solution, but I now experience that
Word still can fool me.

If a different document is activated, then oDoc and ActiveDocument no longer
refer to the same document.
 
B

Bo Hansson

Thanks Jonathan,

Well, I am using Selection objects and will try to change that (puh, quite a
lot of code to replace).

Furthermore, in my application I sometimes change file names by using
SaveAs. After such operations I suppose I have to renew the Set
oDoc=ActiveDocument, right or not ?

If so, I really need to know that the right document is in focus. What about
the solution described in your article "Making a UserForm show above a newly
created Word 2000+ document" ?
Please see my report on trying your code in my way (available in this
tread).

/BosseH
 
J

Jonathan West

Bo Hansson said:
Thanks Jonathan,

Well, I am using Selection objects and will try to change that (puh, quite a
lot of code to replace).

You should be able to do most of it with a simple find replace.

At the start, have these two lines

Dim oRange as Range
Set oRange = Selection.Range

Then everywhere else, just do a straight replace of Selection with oRange.

If you use the Select methof anywhere, then you will need to change those
lines so that

expression.Select

becomes

Set oRange = expression

or possibly

Set oRange = expression.Range

depending on exactly what was being selected before.
Furthermore, in my application I sometimes change file names by using
SaveAs. After such operations I suppose I have to renew the Set
oDoc=ActiveDocument, right or not ?

No, if you do an oDoc.SaveAs, oDoc still stays attached to the same open
document, even though its name has now changed.
 
B

Bo Hansson

Thanks again Jonathan

Knowing to little, one tend to make things more complicated than necessary.

/BosseH
 

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