VBA Code

The mail merge object is always available regardless of whether or not the specified document is a mail merge document
If you haven't added an envelope to the active mailmerge document then using any of the following will cause an error address, addressfromleft, addressfromtop, feedsource, returnaddress, returnaddressfromleft, returnaddressfromtop and updatedocument


Type of Document

This checks to make sure the document is not a mail merge document
The State returns the current state of the mail merge operation. Read-only WdMailMergeState.

If ActiveDocument.MailMerge.State = wdMailMergeState.wdNormalDocument Then 

End If

This checks to make sure that the document is a main document with an attached data source.

If ActiveDocument.MailMerge.State = wdMailMergeState.wdMainAndDataSource Then 

End If


MailMerge Object

Use the MailMerge property to return the MailMerge object.
The MailMerge object is always available regardless of whether the mail merge operation has begun.
Use the State property to determine the status of the mail merge operation.
The following example executes a mail merge if the active document is a main document with an attached data source.


If ActiveDocument.MailMerge.State = wdMailMergeState.wdMainAndDataSource Then 
    ActiveDocument.MailMerge.Execute
End If


The following example merges the main document with the first three data records in the attached data source and then sends the results to the printer.


Set myMerge = ActiveDocument.MailMerge 
If myMerge.State = wdMailMergeState.wdMainAndSourceAndHeader Or _
    myMerge.State = wdMailMergeState.wdMainAndDataSource Then
    With myMerge.DataSource
        .FirstRecord = 1
        .LastRecord = 3
    End With
End If
With myMerge
    .Destination = wdMailMergeDestination.wdSendToPrinter
    .Execute
End With


MailMerge.EditMainDocument

Activates the mail merge main document associated with the specified header source or data source document.
Note If the main document isn't open, an error occurs. Use the Open method if the main document isn't currently open.
This example attempts to activate the main document associated with the active data source document. If the main document isn't open, the Open dialog box is displayed, with a message in the status bar.


Sub ActivateMain() 
    On Error GoTo errorhandler
    Documents("Data.doc").MailMerge.EditMainDocument

    Exit Sub

errorhandler:
    If Err = 4605 Then StatusBar = "Main document is not open"
    Dialogs(wdWordDialog.wdDialogFileOpen).Show
End Sub


MailMerge.Execute

Optional Variant. True for Microsoft Word pause and display a troubleshooting dialog box if a mail merge error is found. False to report errors in a new document.


This example executes a mail merge if the active document is a main document with an attached data source.


Set myMerge = ActiveDocument.MailMerge 
If myMerge.State = wdMailMergeState.wdMainAndDataSource Then
   MyMerge.Execute(Pause:=False)
End If

With ActiveDocument.MailMerge 
   .Destination = wdMailMergeDestination.wdSendToNewDocument
   .SuppressBlankLines = True
   With .DataSource
      .FirstRecord = wdMailMergeDefaultRecord.wdDefaultFirstRecord
      .LastRecord = wdMailMergeDefaultRecord.wdDefaultLastRecord
   End With
   .Execute Pause:=False
End With

MailMerge.Check

Simulates the mail merge operation, pausing to report each error as it occurs.
This example checks the active document for mail merge errors.

ActiveDocument.MailMerge.Check 


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