Insert document body, but not header

S

Strømdahl

I am trying to insert a word document with this code:
Selection.InsertFile FileName:=myfile, Link:=False

It works fine with most documents, but when myfile have a section and uses
header/footer the header from myfile overwrites my origin header. I only want
the main body of myfile and not the header.

I use Word 2003.
 
D

Dave Lett

Hi,

You can use something like the following instead:

Dim myfile As String
Dim oDoc As Document
myfile = "C:\Test.doc"
Set oDoc = Documents.Open(FileName:=myfile, Visible:=False)

Selection.InsertAfter Text:=oDoc.Range.Text
oDoc.Close SaveChanges:=wdDoNotSaveChanges
Set oDoc = Nothing

If you want the formatting of the text in Test.doc to come across, then use
the following:

Dim myfile As String
Dim oDoc As Document
myfile = "H:\Documents\Test.doc"
Set oDoc = Documents.Open(FileName:=myfile, Visible:=False)

Selection.Range.FormattedText = oDoc.Range.FormattedText
oDoc.Close SaveChanges:=wdDoNotSaveChanges
Set oDoc = Nothing

HTH,
Dave
 

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