Word 2007
(1)
VBA
(1)
WdSectionNewPage
(1)
ActiveDocumentSet
(1)
LinkToPrevious
(1)
WdCollapseEnd
(1)
ActiveDocument
(1)
Footers.Count
(1)

Document Assembly in Word 2007

Asked By LA Lawyer
19-Mar-10 05:52 PM
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.

UseDim i As LongDim rng As RangeWith ActiveDocumentSet rng = .Rangerng.

Doug Robbins - Word MVP replied to LA Lawyer
20-Mar-10 01:41 AM
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

This only partially worked.

LA Lawyer replied to Doug Robbins - Word MVP
22-Mar-10 01:11 PM
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.

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

Doug Robbins - Word MVP replied to LA Lawyer
22-Mar-10 07:00 PM
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
Post Question To EggHeadCafe