Merging documents and including headers

M

Michael

Hi all -

I need a macro to merge hundreds of word documents into one large file.
Each document contains a unique header that has critical information. I have
some code that will merge the docs, but it puts the same header on every
single page. It is possible to do this since the info is stored in the
header? I appreciate any suggestions!

code i'm using:

dim Rng as Range
dim Doc as Document
dim strFile as String
dim strPath as String
set strPath = "c:\folder\"
set Doc = Documents.Add
set strFile = dir(strPath)
do until strFile = ""
set Rng = Doc.Range
rng.Collapse wdCollapseEnd
rng.InserFile strPath & strFile
strFile = dir()
Loop
 
J

Jay Freedman

Michael said:
Hi all -

I need a macro to merge hundreds of word documents into one large
file.
Each document contains a unique header that has critical information.
I have some code that will merge the docs, but it puts the same
header on every single page. It is possible to do this since the
info is stored in the header? I appreciate any suggestions!

code i'm using:

dim Rng as Range
dim Doc as Document
dim strFile as String
dim strPath as String
set strPath = "c:\folder\"
set Doc = Documents.Add
set strFile = dir(strPath)
do until strFile = ""
set Rng = Doc.Range
rng.Collapse wdCollapseEnd
rng.InserFile strPath & strFile
strFile = dir()
Loop

The header and footer (as well as the page numbering, margins, page
orientation, and a few other properties) are stored in the section break at
the end of the section to which they apply (). In order to have different
headers, you must insert section breaks between the inserts. Additionally,
when a section break is first inserted, the headers and footers are marked
as "Linked to previous", meaning that the new section automatically shows
the same header or footer as in the preceding section. To have different
headers, you have to turn off that link. If you want the page numbering to
restart in the new section, that's another thing you have to do
specifically.

Try this way:

Sub x()
Dim Rng As Range
Dim Doc As Document
Dim strFile As String
Dim strPath As String

strPath = "c:\folder\"
Set Doc = Documents.Add
strFile = Dir(strPath & "*.*")
Do Until strFile = ""
Set Rng = Doc.Range
Rng.Collapse wdCollapseEnd
Rng.InsertFile strPath & strFile

Rng.Collapse wdCollapseEnd
Rng.InsertBreak Type:=wdSectionBreakNextPage
With Doc.Sections.Last
With .Headers(wdHeaderFooterPrimary)
.LinkToPrevious = False
.PageNumbers.RestartNumberingAtSection = True
.PageNumbers.StartingNumber = 1
End With
.Headers(wdHeaderFooterFirstPage).LinkToPrevious = False
.Headers(wdHeaderFooterEvenPages).LinkToPrevious = False

With .Footers(wdHeaderFooterPrimary)
.LinkToPrevious = False
.PageNumbers.RestartNumberingAtSection = True
.PageNumbers.StartingNumber = 1
End With
.Footers(wdHeaderFooterFirstPage).LinkToPrevious = False
.Footers(wdHeaderFooterEvenPages).LinkToPrevious = False
End With

strFile = Dir()
Loop
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
J

Jay Freedman

Jay said:
The header and footer (as well as the page numbering, margins, page
orientation, and a few other properties) are stored in the section
break at the end of the section to which they apply (). In order to

Oops, meant to include a link to this article:
http://www.word.mvps.org/FAQs/Formatting/WorkWithSections.htm

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
M

Michael

Thank you Jay. That starts me in the right direction. I had to modify your
code just a little to get a new header on each page. I had to change

With Doc.Sections.Last to
With Doc.Sections.Add

I've got some tweaking to do but I think your help has solved my problem.
Thanks so much!!!
 

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