Word - 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.
Word 2007
(1)
VBA
(1)
WdSectionNewPage
(1)
ActiveDocumentSet
(1)
LinkToPrevious
(1)
WdCollapseEnd
(1)
ActiveDocument
(1)
Footers.Count
(1)
  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
  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.
  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
Create New Account
help
Save to PDF using VBA in Word 2007 Word Word 2007 Is it possible to use VBA to save a Word doc in Word 2007. We have installed the 'Save to PDF' add-on. Currently we
Keeping Lines Together (Word 2007 VBA) Word I have a multi-line text variable (an address) that Word 2007 routinely splits between pages. How do I use VBA to instruct Word to keep those lines together? Word VBA Discussions Word 2007 (1) VBA (1) RngAddress.Paragraphs
Unprotect Doc w / word 2007 VBA Word What am I missing. Using word 2007 VBA code: When Using code "ActiveDocument.Unprotect" on a protected document I get an error when I stepped thru the code I got the error message instead of the dialog box. Word VBA Discussions Microsoft Word (1) Word (1) ActiveDocument.Unprotect (1) WdAllowOnlyFormFields (1) VBA (1) ProtectionType
Help with FileSearch in word VBA 2007 VBA Word Hello, I have been using word 2003 with VBA code for macros and custom forms, and everything was fine until I "upgraded" to office 2007 (full install). Now, the following code errors on the Application.Filesearch line and when I
Access 2007 -> Word 2007-> Access 2007 Word Word, 2007-> Access, 2007" / > I posted this in the Access section but have not had much luck. I would