How to switch between two documents

G

guylaine.constant

Hi,

Hi have a macro that does something in the opened "Document A",
then open "Document B",
copy something from Document B
doesn’t close Document B cause I will need it later.
go back to Document A
paste information,
go back to Document B, etc.

While playing the macro, there might be already opened Document C,
Document D, etc.


In this macro, "Document B" is always the same so it's easy to "call"
it. But Document A is not always the same.

So how I can switch between A and B and be sure that I always come
back in the proper document i.e. “Document A”?

I tried by making sure before playing the macro that I have only
Document A open so I could use “Windows(1).Activate” but it works on
and off. Same thing if I write “Windows(2).Activate”

Any help?

Thank you!

S.
 
K

Klaus Linke

Hi,
In this macro, "Document B" is always the same so it's
easy to "call" it.

Yes, you could activate it by addressing it by name:
Documents("Document B.doc").Activate
But Document A is not always the same.

If you make sure that Document A has the focus before you start the macro,
then you can set a variable to it right away:

Dim docA as Document
Set docA = ActiveDocument
....
Documents("Document B.doc").Activate
....
docA.Activate

Klaus
 
J

Jay Freedman

Alternately activating two documents and using copy/paste between them is
the most inefficient and error-prone method possible. It is, unfortunately,
the only kind of code you can get from the macro recorder. But it should be
marked with a big "DO NOT USE" warning.

Instead, declare and assign two Document objects to represent the two
documents. Then you can use two Range objects, one in each document, to
transfer the text without going through the Windows clipboard. Here's some
very elementary sample code:

Sub demo()
Dim SrcDoc As Document
Dim DestDoc As Document
Dim SrcRg As Range
Dim DestRg As Range

' assign the currently open document (Doc A)
' as the "destination" for the copying
Set DestDoc = ActiveDocument

' open an existing document (Doc B)
' as the "source" for the copying
Set SrcDoc = Documents.Open("C:\temp\DocB.doc")

' locate the place in Doc B that you want to copy
' (only you know what this is...)
Set SrcRg = SrcDoc.Range
SrcRg.Find.Execute FindText:="copy me"

If SrcRg.Find.Found Then
' locate the place in Doc A to copy to
Set DestRg = DestDoc.Range
DestRg.Collapse wdCollapseEnd

' transfer the text
DestRg.FormattedText = SrcRg.FormattedText
End If
End Sub

To copy more than one bit of text, you'll need to put the range manipulation
into a loop with some logic that decides whether to continue to loop or jump
out of it. At the end of the macro you may want to close SrcDoc and save
DestDoc. The whole thing needs plenty of error handling.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

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