Changing the ActiveDocument mid stream

D

Dave

G'day,

I am dumping information into MS Word 2000 from MS Project
98 via VBA code. I am doing this to allow me to present
certain data in a report format that suits our
requirements.

This "report" could be run numerous times in a short
period by the user. It works well the first time. However,
if it is run a second time (while the first one is still
open), or even if another Word file is open, it causes an
error towards the end of the process. The code refers to
the ActiveDocument in numerous places, which it seems to
cope with, but for some reason it changes to start working
on the other open Word document and this causes an error
because it is not finding what it is expecting.

I am wondering if there is some way to definitely refer to
the new document or to kill the connection to any other
open Word documents without having to close them (they may
want them both open at the one time to compare
information).

Any help would be greatly appreciated.

(I wasn't sure whether to post this to the MS Word or MS
Project user groups, but thought it was more of a Word
issue).

Thanks,

Dave.
 
H

Helmut Weber

Hi Dave,
if working with the activedocument goes wrong
working with e.g. documents(1) etc. or with
creating a new object and assigning the
activedocument to it, (useless anyway)
may go wrong, too. I'd think closing all
other documents than the relevant one
and open them again afterwards might be
a very bullet proof solution to prevent
word from writing to another doc than the
one and only that is there.
 
P

Pete Bennett

Try this...
Dim aDoc as Document
Dim bDoc as Document

Set aDoc=Documents.Add ' or whatever
Set bDoc = Documents (2) ' or whatever


Then, whenever you want to add information to the end of either document use

aDoc.Range.InsertAfter "text appended"

and

bDoc.Range.InsertAfter "text in other document"

That way, you don't uve ActiveDocument at all and you lose any amibiguity
about what you're doing... Because you're keeping handles to each document
and you work with those handles, they won't ever get mixed up.

It's only when you use ActiveDocument and Selection opbjects without
referencing which document is actually active that you start running into
problems.

Hope that's useful,

Pete.
 

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