Keeping track of document windows

J

Jay

I'm wrting some VBA (using Windows NT and Word 97) that finds and
copies a few things from one document into a new (created by VBA)
document. I use...

winSrc = ActiveWindow.Index
Documents.Add
winDst = ActiveWindow.Index
Do
ActiveDocument.Windows(winSrc).Activate
'find something
...
...
Selection.copy
ActiveDocument.Windows(winDst).Activate
Selection.Paste
Loop while ...

The problem is that winSrc is assigned to 1, but when Documents.Add
adds a new document, this becomes Window number 1 so winDst is
assigned to 1 also. In the Word Windows menu, the original window is
renumbered to Window number 2, which seems strange but that's the way
VBA seems to work so I have to live with it. The result is that my
code doesn't work.

How do I find what my existing document is re-assigned to in VBA? In
this case, I know it's 2, but if I have several documents open, I
can't make this assumption. Alternatively, is there a better way to do
what I'm trying to do?
 
J

Jonathan West

Hi jay,

First of all, word from documents rather than dinwos, it will be much easier
to keep track of things.

Dim docSource as Document
Dim docDest as Document
Set docSource = ActiveDocument
Set docDest = Documents.Add

Once you assign an object variable to a document it will continue to
reference that document no matter whether the new documents are created, or
whether different documents are activated. You can do almost anything with
docSource or docDest that you can do with ActiveDocument.

Second, you can transfer formatted text between documents without using the
clipboard. Look up the FormattedText property in VBA help.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
J

Jay

Thanks Jonathan, all is working well now.

Jonathan West said:
Hi jay,

First of all, word from documents rather than dinwos, it will be much easier
to keep track of things.

Dim docSource as Document
Dim docDest as Document
Set docSource = ActiveDocument
Set docDest = Documents.Add

Once you assign an object variable to a document it will continue to
reference that document no matter whether the new documents are created, or
whether different documents are activated. You can do almost anything with
docSource or docDest that you can do with ActiveDocument.

Second, you can transfer formatted text between documents without using the
clipboard. Look up the FormattedText property in VBA help.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

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