changing code to also copy the header and footer 2 saved docs

S

S trainer

The following code works really well for splitting a merged document into
separate documents and saving them as files; but the header and footer
information from the original merged document is missing in the individual
documents. Is it possible to have the footer and header from the original
merge document appear in the individual documents?


Sub MailMergeLetterSplitter()
'
' MailMergeLetterSplitter Macro
' Macro created 1/24/2007 by solsenb
'
' splitter Macro

' Macro created by Doug Robbins to save each letter created by a mailmerge
as a separate file.

Dim i As Long, Source As Document, Target As Document, Letter As Range
Set Source = ActiveDocument
For i = 1 To Source.Sections.Count
Set Letter = Source.Sections(i).Range
Letter.End = Letter.End - 1
Set Target = Documents.Add
Target.Range = Letter
Target.SaveAs FileName:="Letter" & i
Target.Close
Next i

End Sub
 
J

Jezebel

Simplest approach is to create a template that has the headers and footers
you want, then create your new documents using that template --

Set Target = Documents.Add(Template:="myTemplate.dot")
 
S

S trainer

Jezebel,

Thank you for answering so quickly.

The main merge document all ready has the header and footer needed, the
merged document (all of the letters in one document) has the header and
footer needed, but through the processing of the code below, which separates
each of the letters from the merged document and saves them each to a
separate file, the header and footer are missing in the separated files.

Any ideas to keep the header and footer in the separated letters?

Thanks.
 
D

Doug Robbins - Word MVP

The Header/Footer information is contained in the Section Break that appears
at the end of each merged letter. As for all merged letters, other than the
last, the Section Break is a Next Page Section Break, the following line of
code:

Letter.End = Letter.End - 1

was used to delete that Section Break so that there would not be an empty
page at the end of each separated letter.
By deleting the Section Break, the header and footer are being deleted. The
following modified code, converts each Section Break to a Continuous Section
Break, that will eliminate the empty page (unless the previous page is full
to the gills), rather than delete the Section Break.

Dim i As Long, Source As Document, Target As Document, Letter As Range
Set Source = ActiveDocument
For i = 1 To Source.Sections.Count
Set Letter = Source.Sections(i).Range
Set Target = Documents.Add
Target.Range = Letter
Target.Sections(Last).PageSetup.SectionStart = wdSectionContinuous
Target.SaveAs FileName:="Letter" & i
Target.Close
Next i


--
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
 
S

S trainer

Thank you again Doug!

Doug Robbins - Word MVP said:
The Header/Footer information is contained in the Section Break that appears
at the end of each merged letter. As for all merged letters, other than the
last, the Section Break is a Next Page Section Break, the following line of
code:

Letter.End = Letter.End - 1

was used to delete that Section Break so that there would not be an empty
page at the end of each separated letter.
By deleting the Section Break, the header and footer are being deleted. The
following modified code, converts each Section Break to a Continuous Section
Break, that will eliminate the empty page (unless the previous page is full
to the gills), rather than delete the Section Break.

Dim i As Long, Source As Document, Target As Document, Letter As Range
Set Source = ActiveDocument
For i = 1 To Source.Sections.Count
Set Letter = Source.Sections(i).Range
Set Target = Documents.Add
Target.Range = Letter
Target.Sections(Last).PageSetup.SectionStart = wdSectionContinuous
Target.SaveAs FileName:="Letter" & i
Target.Close
Next i


--
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
 

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