Macro for Mail Merging

G

Guus

Hello,
I frequently have to create Mail Merge results, so I want to create a macro
for this.
The macro has to do the following tasks:
1. Open my Main Document, with is always the same.
2. Connect the main document to a Data Source (because it changes all the
time,
I want to show a Dialog Box for selecting the Data Source.
3. Then I do the Merge to a New Blank Document
4. I want to save the merged document, here I want to show again a Dialog Box
5. At the end, I want to close the Main Document without saving.

The macro to be completed looks like this:

Dim Folder As String
Dim SourceFile As String

Dim fd As FileDialog

Folder = "D:\Feedback\"

'Standard directory
ChangeFileOpenDirectory Folder

'Open the existing template
Documents.Open Filename:="Primary_Document.doc", _
ConfirmConversions:=True, _
AddToRecentFiles:=False, Format:=wdOpenFormatAuto

'Show the Toolbar "Mail Merge"
CommandBars("Mail Merge").Visible = True

'Configure the Main Document
'1. TYPE
ActiveDocument.MailMerge.MainDocumentType = wdFormLetters

'2. DATA SOURCE (MY PROBLEM IS "HOW TO SHOW A DIALOGBOX" !!!!!)

'3. Execute the merging
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
.DataSource.FirstRecord = wdDefaultFirstRecord
.DataSource.LastRecord = wdDefaultLastRecord
.Execute Pause:=False
End With

'4. Save the merged data
Dialogs(wdDialogFileSaveAs).Show

'5. Close the template
Windows("Primary_Document.doc").Activate
ActiveDocument.Close

Who can help me to complete the VBA code, specially for showing the Dialog
Box as mentioned in line "2." and "4."?

Thank you in advance,
Guus
 
D

Doug Robbins - Word MVP

Dialogs(wdDialogMailMergeOpenDataSource).Show

will display the Open Data Source dialog. However, you should use it in a
construction like

If ActiveDocument.MailMerge.MainDocumentType <> wdNotAMergeDocument Then
With Dialogs(wdDialogMailMergeOpenDataSource)
If .Show Then
'The rest of your code
Else
MsgBox "Attaching Data Source cancelled by User."
Exit Sub
End If
End With
Else
MsgBox "The Active Document is not a Mail Merge Main Document."
Exit Sub
End If


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