Header Sources


MailMerge.CreateHeaderSource

Creates a Microsoft Word document that stores a header record that's used in place of the data source header record in a mail merge. This method attaches the new header source to the specified document, which becomes a main document if it's not one already.


Note The new header source uses a table to arrange mail merge field names.


Security Avoid using hard-coded passwords in your applications. If a password is required in a procedure, request the password from the user, store it in a variable, and then use the variable in your code. For recommended best practices on how to do this, see Security Notes for Microsoft Office Solution Developers.


expression.CreateHeaderSource(Name, PasswordDocument, WritePasswordDocument, HeaderRecord)
expression Required. An expression that returns a MailMerge object.


Name Required String. The path and file name for the new header source.


PasswordDocument Optional Variant. The password required to open the new header source.


WritePasswordDocument Optional Variant. The password required to save changes to the new header source.


HeaderRecord Optional Variant. A string that specifies the field names for the header record. If this argument is omitted, the standard header record is used: "Title, FirstName, LastName, JobTitle, Company, Address1, Address2, City, State, PostalCode, Country, HomePhone, WorkPhone." To separate field names in Windows, use the list separator specified in Regional Settings in Control Panel.


This example creates a header source with five field names and attaches the new header source named "Header.doc" to the active document.

ActiveDocument.MailMerge.CreateHeaderSource Name:="Header.doc", _ 
    HeaderRecord:="Name, Address, City, State, Zip"

This example creates a header source for the document named "Main.doc" (with the standard header record) and opens the data source named "Data.doc."

With Documents("Main.doc").MailMerge 
    .CreateHeaderSource Name:="Fields.doc"
    .OpenDataSource Name:="C:\Documents\Data.doc"
End With

MailMerge.EditHeaderSource

Opens the header source attached to a mail merge main document, or activates the header source if it's already open.
Note If the mail merge main document doesn't have a header source, this method causes an error.


This example attaches a header source to the active document and then opens the header source.


With ActiveDocument.MailMerge 
    .MainDocumentType = wdFormLetters
    .OpenHeaderSource Name:="C:\Documents\Header.doc"
    .EditHeaderSource
End With

This example opens the header source if the active document has an associated header file attached to it.


Dim mmTemp As MailMerge 

Set mmTemp = ActiveDocument.MailMerge
If mmTemp.State = wdMainAndSourceAndHeader Or _
        mmTemp.State = wdMainAndHeader Then
    mmTemp.EditHeaderSource
End If

MailMerge.OpenHeaderSource

Attaches a mail merge header source to the specified document.


expression.OpenHeaderSource(Name, Format, ConfirmConversions, ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate, OpenExclusive)
expression Required. An expression that returns a MailMerge object.


Name Required String. The file name of the header source.


Format Optional Variant. The file converter used to open the document. Can be one of the following WdOpenFormat constants. To specify an external file format, use the OpenFormat property with a FileConverter object to determine the value to use with this argument.


WdOpenFormat can be one of these WdOpenFormat constants.


ConfirmConversions - Optional Variant. True to display the Convert File dialog box if the file isn't in Microsoft Word format.
ReadOnly - Optional Variant. True to open the header source on a read-only basis.
AddToRecentFiles - Optional Variant. True to add the file name to the list of recently used files at the bottom of the File menu.
PasswordDocument - Optional Variant. The password required to open the header source document. (See Remarks below.)
PasswordTemplate - Optional Variant. The password required to open the header source template. (See Remarks below.)
Revert - Optional Variant. Controls what happens if Name is the file name of an open document. True to discard any unsaved changes to the open document and reopen the file; False to activate the open document.
WritePasswordDocument - Optional Variant. The password required to save changes to the document data source. (See Remarks below.)
WritePasswordTemplate - Optional Variant. The password required to save changes to the template data source. (See Remarks below.)
OpenExclusive - Optional Variant. True to open exclusively.


Remarks
When a header source is attached, the first record in the header source is used in place of the header record in the data source.
Security Avoid using hard-coded passwords in your applications. If a password is required in a procedure, request the password from the user, store it in a variable, and then use the variable in your code. For recommended best practices on how to do this, see Security Notes for Microsoft Office Solution Developers.


This example sets the active document as a main document for form letters, and then it attaches the header source named "Header.doc" and the data document named "Names.doc."


With ActiveDocument.MailMerge 
    .MainDocumentType = wdFormLetters
    .OpenHeaderSource Name:="C:\Documents\Header.doc", _
         Revert:=False, AddToRecentFiles:=False
    .OpenDataSource Name:="C:\Documents\Names.doc"
End With


© 2020 Better Solutions Limited. All Rights Reserved. © 2020 Better Solutions Limited TopPrevNext