Document Assembly in Word 2007

L

LA Lawyer

I am trying to assemble a big document in Word using VBA.

The document already on the screen has footers and headers. I want to add
another document to follow it which does not need those headers or footers.
Here is my code, which is intuitive, but clearly wrong:

With Selection
.MoveEnd Unit:=wdStory, Count:=1
.InsertBreak Type:=wdSectionBreakNextPage
.InsertFile FileName:="n:\WordForms\Driving Instructions.docx",
Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False
End With

I am attempting to have VBA insert this already-existing additional document
on a new page at the end of the active document. Instead, VBA keeps the
original header and footer, deletes all of the non-header text and then
inserts the new document there.

What am I doing wrong? I presume that this is a common procedure.
 
D

Doug Robbins - Word MVP

Use

Dim i As Long
Dim rng As Range
With ActiveDocument
Set rng = .Range
rng.Collapse wdCollapseEnd
.Sections.Add rng, wdSectionNewPage
Set rng = .Range
rng.Collapse wdCollapseEnd
rng.InsertFile "n:\WordForms\Driving Instructions.docx"
With rng.Sections(1)
For i = 1 To .Headers.Count
.Headers(i).LinkToPrevious = False
.Headers(i).Range.Delete
Next i
For i = 1 To .Footers.Count
.Footers(i).LinkToPrevious = False
.Footers(i).Range.Delete
Next i
End With
End With


--
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, originally posted via msnews.microsoft.com
 
L

LA Lawyer

This only partially worked.

This DID preserve the header/footer from the first page but deleted all of
the text on the regular part of the first page and replaced it with the text
of the document which which was inserted.

I need to preserve the text of the first page.
 
D

Doug Robbins - Word MVP

It does not delete the text from the original document when I use it here.

--
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, originally posted via msnews.microsoft.com
 

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