PDA

View Full Version : [SOLVED:] Mailmerge macros: File naming from header



zcty
01-26-2018, 07:50 AM
I run the following macro to separate the mailmerged document into individual documents, the file names are as x iteration. So 1,2,3..
I would like however to replace the filename and extract the header of the active file so each new file would have its individual header used as a file name. Ive tried replacing x as you may see below, to no avail. any ideas?

I use the following code to separate from mail merged doc to individual docs. Works like a charm:

Option Explicit

Sub AllSectionsToSubDoc()

Dim x As Long
Dim Sections As Long
Dim Doc As Document

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Set Doc = ActiveDocument
Sections = Doc.Sections.Count
For x = Sections - 1 To 1 Step -1
Doc.Sections(x).Range.Copy
Documents.Add
ActiveDocument.Range.Paste
ActiveDocument.SaveAs (Doc.Path & "\" & x & ".doc")
ActiveDocument.Close False
Next x

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

The next code is what Ive tried to extract the header;
(basically the abive, replacing x in the save as line.)



Option Explicit
Sub AllSectionsToSubDoc()
Dim x As Long
Dim Sections As Long
Dim Doc As Document
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set Doc = ActiveDocument
Sections = Doc.Sections.Count
For x = Sections - 1 To 1 Step -1
Doc.Sections(x).Range.Copy
Documents.Add
ActiveDocument.Range.Paste
ActiveDocument.SaveAs (Doc.Path & "\" & ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader & ".doc")
ActiveDocument.Close False
Next x
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub


This didnt work -> syntax error in line 18, any ideas?

Thanks in advance!:cool:

macropod
01-26-2018, 06:12 PM
It's not apparent what you mean by 'the header of the active file' in this case. It could be the mailmerge source header, a page header, or something else. That said, see:
• Send Mailmerge Output to Individual Files;
• Split Merged Output to Separate Documents
in in the MailmergeTips and Tricks thread at:
http://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html
or:
http://windowssecrets.com/forums/showthread.php/163017-Word-Mailmerge-Tips-amp-Tricks

zcty
02-05-2018, 05:21 AM
Hi Paul,

Thank you very much for your reply and help. Your posts have lots of great help.

My issue is saving the merged document into individual documents with the output filename being the header of the document.

Using the code above I have managed to separate the merged document into individual documents, which are saved however output filename 1, 2, 3.... Instead of x in the code above therefore I would like the output file ame of individual documents to contain the header of each document.

I have looked at the links you have attached in the section;

'Split Merged Output to Separate Documents' You have addressed my issue by mentioning how to change the desired output filename, however I still dont know how to use the header.

For instance;
As coded, it is assumed the output filename consists of the first paragraph in each record. If not, you could use a different range or replace all of the content between the ***** strings with code like
Code:
' Construct the destination file path & name
StrTxt = ActiveDocument.Path & Application.PathSeparator & (i + j - 1) / j


OR ' Construct the destination file path & name
StrTxt = ActiveDocument.Path & Application.PathSeparator & StrTxt

Instead I would like to add the Header

Any tips for this?

Many Thanks!

Z :)

macropod
02-05-2018, 01:26 PM
All you may need to do is change:
Set Rng = .Range.Paragraphs(1).Range
to, say:
Set Rng = .Headers(wdHeaderFooterPrimary).Range.Paragraphs(1).Range

zcty
02-06-2018, 02:22 AM
Cheers Paul.

That's all it needed. Issue solved.

Thanks,
Z