Copy and Paste

D

Don

Hello!

I am writing a system in Access 2002 (I am an experienced
programmer but new to Automation) One of the functions of
this project is to create a report in Word from a
combination of data and related Word documents using two
templates - one for the report header and one for the
detail. I have been able to successfully create each of
the documents from the templates, but I don't know how to
blend the two together. Here is the scenario:

I start Word and create a document (oDocMain) from my
header template. (Successful)

I open my detail template and create a document
(oDocDetail) and populate it with detail data using
bookmarks and a database record. (Successful)

Next I want to select all the text (including formatting)
from oDocDetail and paste it at the end of the oDocMain.

I will repeat this behavior through a loop statement
until all the detail records have been read and save the
oDocMain as the report.

Can someone give me the coding needed to select all the
text in the oDetailDoc and copy and paste it into
oDocMain? This is what I'm stuck on. I have the rest of
the process working.

Thanks TONS in advance!

Don
 
P

Peter Hewett

Hi Don

The following code will do what you want:

Public Sub CopyToMainDocument()
Dim rngDetail As Word.Range
Dim rngMain As Word.Range

' Source and destination for the copy
Set rngDetail = oDetailDoc.Content
Set rngMain = oDocMain.Content

' Copy the document content
rngMain.FormattedText = rngDetail.FormattedText
End Sub

HTH + Cheers - Peter
 
G

Guest

Hi, Peter!

That's very close, but not quite. What happens in that
scenario is that the main document is completely replaced
each time the routine is called, leaving me with just the
last detail record information. I need to append the
oDocDetail contents to the end of oDocMain each time. Can
you tell me how to append instead of replace the detail
each time?

Thanks!
Don
 
P

Peter Hewett

Hi <[email protected]>

You maybe using Access but you should still be creating a new document from a template.
That way the new document will always empty. This is true even if you throw the document
you create away rather than save it.

Anyway, to change the code just change:
Set rngMain = oDocMain.Content

to
Set rngMain = oDocMain.Content
rngMain.Collapse wdCollapseEnd

HTH + Cheers - Peter
 
B

Bruce Brown

Peter -

Would you be kind enough to distinguish between a document object's
..Content and .Range and describe the different circumstances under
which you would use each? At a glance, their properties and methods
seem to be exactly the same.

Many thanks. - Bruce
 
P

Peter Hewett

Hi Bruce Brown

A Documents Content property returns a Range object which maps the entire Main document
story. That's why it has a Range objects properties and methods!

In fact you could write:
Set rngMain = ActiveDocument.Content

as:
Set rngMain = ActiveDocument.StoryRanges(wdMainTextStory)

They are exactly the same and both return range objects.

HTH + Cheers - Peter


(e-mail address removed) (Bruce Brown), said:
 
B

Bruce Brown

Peter - Sorry, maybe I'm being thick-headed but I still don't see any
difference between a document's .Range and .Content. Playing with the
examples you gave, all three seemed to do the same thing
interchangeably, that is, .Content, .StoryRanges(wdMainTextStory) or
..Range.

Is there any situation where you could use either .Content or .Range
but not the other? - Bruce
 
P

Peter Hewett

Hi Bruce Brown

The differences with a Document objects Range is subtle. Let me try to explain:

..Content and .StoryRanges are actually properties that return a range object but .Range is
a method that returns a Range object! The Range method can take two arguments Start and
End. So if you just want the characters 101 through 200 of your document you can do this:

Dim rngA As Word.Range
Set rngA = ActiveDocument.Range(101 200)

instead of
Dim rngA As Word.Range
Set rngA = ActiveDocument.Content
rngA.Start = 101
rngA.End = 200

The reason that the Range method appears to work the same as .Content and .StoryRanges, as
in this example:

Set rngA = ActiveDocument.Content
Set rngA = ActiveDocument.StoryRanges(wdMainTextStory)
Set rngA = ActiveDocument.Range

Is that the Range methods 2 arguments are optional. If both are omitted as above the
Start has a value of 1 and End has a value equivalent to the number of characters in the
document.

The Range method allows you to specify which range of characters to return, whereas the
..Content and .StoryRanges properties always return all of the documents Main story text.

HTH + Cheers - Peter


(e-mail address removed) (Bruce Brown), said:
 

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