Is this possible

T

Tom

Using Word 2002, using vba we would like to copy the text
from 1st open document, paste to 3rd document, then copy
the text from 2nd open document and paste after the 1st
documents text on 3rd document. The 3rd document is then
saved.

If this is possible guidance on code would be appreciated

TIA
Tom
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Tom > écrivait :
In this message said:
Using Word 2002, using vba we would like to copy the text
from 1st open document, paste to 3rd document, then copy
the text from 2nd open document and paste after the 1st
documents text on 3rd document. The 3rd document is then
saved.

If this is possible guidance on code would be appreciated
Yes.

To make sure it goes smoothly, declare 3 document object
Dim Doc1 As Document
Dim Doc2 As Document
Dim Doc3 As Document
and 3 range object
Dim Range1 As Range
Dim Range2 As Range
Dim Range3 As Range

Then assign the documents to the Document variables
Set Doc1 = Application.Documents(1)
Set Doc2 = Application.Documents(2)
Set Doc3 = Application.Documents(3)
It is better to use Document names so s to avoid errors, like:
Set Doc3 = Application.Documents("Document3.doc")

Then go from there.

Define a range in Doc1.
e.g.:

With Doc1.Range
Set Range1 = Doc1.Range(.Paragraphs(1).Range.Start, _
.Paragraphs(3).Range.End).FormattedText
End With

If you want the full text:

Set Range1 = Doc1.Range.FormattedText

Finally,

Set Range3 = Doc3.Range
Range3.Collapse wdCollapseEnd
Range3.FormattedText = Range1
Range3.Collapse wdCollapseEnd

And repeat for Range2.

Using Document and Range objects you are 100% in control.
Try not to use the Selection object which can get really messy when going
form document to document...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Guest

Thank you Jean-Guy for your advice. If we wanted to
refine the required copy and paste and have a carriage
return and then the 2nd copy & paste, how would the
carriage return be introduced into the code

Tom
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < (e-mail address removed) > écrivait :
In this message said:
Thank you Jean-Guy for your advice. If we wanted to
refine the required copy and paste and have a carriage
return and then the 2nd copy & paste, how would the
carriage return be introduced into the code

Something like (Modifying the previously posted code):

'_______________________________________
Dim Doc1 As Document
Dim Doc2 As Document
Dim Doc3 As Document
Dim Range1 As Range
Dim Range2 As Range
Dim Range3 As Range

Set Doc1 = Application.Documents(3)
Set Doc2 = Application.Documents(2)
Set Doc3 = Application.Documents(1)
'or
'Set Doc3 = Application.Documents("Document3.doc")

Set Range1 = Doc1.Range.FormattedText
Set Range2 = Doc2.Range.FormattedText
Set Range3 = Doc3.Range

With Range3
.Collapse wdCollapseEnd
.FormattedText = Range1
.InsertParagraphAfter
.Collapse wdCollapseEnd
.FormattedText = Range2
End With


Set Doc1 = Nothing
Set Doc2 = Nothing
Set Doc3 = Nothing
Set Range1 = Nothing
Set Range2 = Nothing
Set Range3 = Nothing
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.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