Copying Only the Body Text To Paste Into Another Document?

P

Phil Galey

I have the following code in VB6 and am trying to copy the body text from
one document to another document. The problem I'm having is that not only
the body text is copied, but the header and footer information are also
copied. In my situation, the target document will have it's own header and
footer that are different than the original document. Thus only the body
text (with formatting) must be copied.

--------------------------------------------------------------------
Dim WApp As New Word.Application
Dim WDoc1 As Word.Document
Dim WDoc2 As Word.Document

Set WDoc1 = WApp.Documents.Open("<Path to document>")
WDoc1.StoryRanges(wdMainTextStory).Copy
Set WDoc2 = WApp.Documents.Add
WDoc2.StoryRanges(wdMainTextStory).Paste
WApp.Visible = True
Stop

WApp.Documents.Close False
WApp.Quit
 
D

Doug Robbins - Word MVP

Try

Dim WApp As New Word.Application
Dim WDoc1 As Word.Document
Dim WDoc2 As Word.Document
Dim Wrange as Range

Set WDoc1 = WApp.Documents.Open("<Path to document>")
Set Wrange = WDoc1.Ranges
Wrange.End = Wrange.End - 1
Set WDoc2 = WApp.Documents.Add
WDoc2.Range = Wrange
WApp.Visible = True
Stop

WApp.Documents.Close False
WApp.Quit

See the article "How is it possible to copy an entire document into another
document without bringing across the header and footer?" at:

http://www.word.mvps.org/FAQs/Formatting/PasteWithoutSectionInfo.htm


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - 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