PDA

View Full Version : Automating Mail Merge



bj_au
05-05-2005, 07:07 PM
I need to create a mail merge document that by default, opens to the step in the mail merge wizard where you can edit the recipient list before sending the merged records to a new document (or returning to the normal mail merge wizard steps).

Thanks

sandam
05-06-2005, 01:02 AM
shouldn't be too difficult, basically you need to create a template and in the AutoNew() sub, set the data source and start the mailmerge wizard.

What version of office are you working in?

MOS MASTER
05-06-2005, 02:54 AM
Hi and welcome to VBAX! :hi:

You can open the Wizard in any step you'd like.

Open your mergedocument and put this code in a new code module:
Option Explicit
Sub AutoOpen()
Call ShowMailMergeWizard(objDocument:=ActiveDocument, _
intFirstStep:=3, blnSelectDocType:=False, _
blnPreviewDoc:=False)
End Sub

Function ShowMailMergeWizard(ByRef objDocument As Document, _
Optional ByVal intFirstStep As Integer = 1, _
Optional ByVal blnSelectDocType As Boolean = True, _
Optional ByVal blnSelectStartingDoc As Boolean = True, _
Optional ByVal blnSelectRecipients As Boolean = True, _
Optional ByVal blnWriteDoc As Boolean = True, _
Optional ByVal blnPreviewDoc As Boolean = True, _
Optional ByVal blnCompleteMerge As Boolean = True) As Boolean
On Error Resume Next
Const WIZARD_NOT_OPEN As Integer = 0
With objDocument.MailMerge
If .WizardState = WIZARD_NOT_OPEN Then
.ShowWizard InitialState:=intFirstStep, _
ShowDocumentStep:=blnSelectDocType, _
ShowTemplateStep:=blnSelectStartingDoc, _
ShowDataStep:=blnSelectRecipients, _
ShowWriteStep:=blnWriteDoc, _
ShowPreviewStep:=blnPreviewDoc, _
ShowMergeStep:=blnCompleteMerge
ShowMailMergeWizard = True
Else
'Indicates the wizard is already open and no changes
'are made.
ShowMailMergeWizard = False
End If
End With
End Function


To change the step in to the step you want go to the AutoOpen procedure then change the intFirstStep argument to the step you like!

The code will run when you open the document. (Double click)

Enjoy! :whistle:

bj_au
05-08-2005, 04:36 PM
Thanks heaps. Works like a charm. I was looking for one final step in this sequence, which actually calls up the dialog box that enables you to select or deselect mail recipients (having the same effect as pressing the 'Edit Recipient List' command within the mail merge wizard.

Thanks again

MOS MASTER
05-09-2005, 06:18 AM
Hi,
You're welcome! :beerchug:

For my understanding what is a 'Heaps'?

To your question change the AutoOpen sub to this one to end with the dialog:
Sub AutoOpen()
Call ShowMailMergeWizard(objDocument:=ActiveDocument, _
intFirstStep:=3, blnSelectDocType:=False, _
blnPreviewDoc:=False)

Dialogs(wdDialogMailMergeRecipients).Show
End Sub


Enjoy! :whistle:

sandam
05-09-2005, 07:32 AM
Joost

"thanks heaps" is die selfde as dankie baie.

Andrew;?

MOS MASTER
05-09-2005, 07:48 AM
Joost

"thanks heaps" is die selfde as dankie baie.

Andrew;?
Hi Andy, :D

Ah ok dank je! :thumb

bj_au
05-09-2005, 04:51 PM
Ah thanks again.

Heaps means 'alot', but I'll hazard a guess and say 'Danke Baie'.

sandam
05-10-2005, 01:11 AM
Glad VBAX could help bj_au. Don't forget to mark your threads solved using the Thread Tools at the top of the page.

Andrew;?

MOS MASTER
05-10-2005, 09:57 AM
Ah thanks again.

Heaps means 'alot', but I'll hazard a guess and say 'Danke Baie'.
Hi Bj, :D
You're Welcome! :beerchug: