copying from one document to another

R

Russ

I am trying to copy an entire document and add it to the end of another
document. I know this is trivial but I just can't seem to find the code
equivalent of the following that does not use selection

Selection.HomeKey Unit:=wdStory 'go to the beginning
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Selection.Copy

I paste at the destination document with this code

Set range2 = Documents("destination.doc").Content 'paste data into big
document at the end of last paste
range2.Collapse Direction:=wdCollapseEnd
range2.Paste

Anyone help me on this?
 
P

Pesach Shelnitz

Hi Russ,

The following macro demonstrates how to find the beginning and end of a doc
using a Range object.

Sub RangeToStartAndEnd()
Dim myRange As Range

Set myRange = ActiveDocument.Range
myRange.Collapse Direction:=wdCollapseStart
myRange.Select
MsgBox "This is the beginning."
myRange.Expand wdStory
myRange.Collapse Direction:=wdCollapseEnd
myRange.Select
MsgBox "This is the end."
Set myRange = Nothing
End Sub

With regard to your ultimate goal, consider using the InsertFile method
instead of Copy/Paste.
 
R

Russ

Hi Pesach,

I used your code in the following way:

Set myRange = Documents("doc to be copied").Range
myRange.Collapse Direction:=wdCollapseStart
myRange.Select
myRange.Expand wdStory
myRange.Collapse Direction:=wdCollapseEnd
myRange.Select
myRange.copy
set range2 = Documents("destination.doc").Content
range2.collaspse Direction:=wdCollapseEnd
range2.Paste
set myRange = Nothing
set range2 = Nothing
When the code reaches MyRange.copy I get an error message that there is
nothing to copy even thought the document "Doc to be copied" has a table.
What am i missing?
 
R

Russ

Let me be a little clearer on what I am trying to do. Actually I have
already done it but want to change the code to not use the selection method.
This is not hard and is something people must do all the time. I have three
documents, two of which are really templates. In each template document I
have a table followed by some text. At the beginning of the macro I open the
two template documents and then load a Word VBA array with data from an Excel
spreadsheet. You can think of this data as records. Depending on the record
type I use one of the two templates, filling the table in the template with
data from the array. After each record is loaded I want to copy the template
to the final document. The final document contains all the code for the
macro. Sometimes I just copy the table which is easy. I simply use:
Documents(“template1.docâ€).tables(1).Range.Copy
Set range2 = Documents(“final documentâ€).Content
Range2.collapse Direction:=weCollapseEnd
Range2.paste

But sometimes I copy the table plus the text that follows:

For these cases I want to form a range that includes the table and the text
that follows which never changes. Using the selection method I did this with
this code:

Selection.HomeKey Unit:=wdStory 'go to the beginning
All of this worked fine. I am trying to replace this code with code that
does not use the selection method.
I hope this better explains what I am looking for. This has to be trivial
but it is escaping me. I have written tons of VBA code for Excel but this is
my only venture into Word VBA.
 
G

Graham Mayor

Do you mean like

Documents("template1.doc").Range.Copy
Documents("final document").Range.Paste


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
R

Russ

Graham,
My God it was that simple. The line:
Documents("template1.doc").Range.Copy
was the line I was looking for.
The only thing I can say in my defense is that I said I thought it was
"trivial". It was just more trivial then even I imagined.
Thanks a million.
 
G

Graham Mayor

There are times when we all don't see the wood for the trees ;)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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