How to automate MailMerge and suppress error and other messages

A

Al

I have an process I am developing which will read all the document names in a
folder, process them one-by-one doing a mail merge with the same Excel
spreadsheet, saving the merged document to a set folder, close the document
and process the next document. Some folders have 80 to 700 documents.

If I run into errors I really don't care as the next process will pick that
up, I just want to open a document, merge, save as another document. How can
I do this, currently I get errors that ask me to connect to the Excel
spreadsheet, and when a merge field on a document doesn't exist in the
spreadsheet it stops and wants me to do something with it.

Below is the code I am using in the PrintDocument Function:

strSaveName = "C:\ADOCS\" & strSaveName

'Open, Merge, Save New Document, and Close the template file
appWord.Documents.Open (strDoc)
With appWord.ActiveDocument.MailMerge
.SuppressBlankLines = True
.OpenDataSource Name:=Application.CurrentProject.Path &
"\MergeData.xls", LinkToSource:=True, SQLStatement:="SELECT * FROM
`tblMergeData`"
.Destination = wdSendToNewDocument
.Execute
End With
appWord.ActiveDocument.Fields.Update
appWord.ActiveDocument.SaveAs strSaveName
appWord.ActiveDocument.Close savechanges:=0

Exit_Here:
Exit Function

=======================================
Thanks for your assistance!
 
D

Doug Robbins - Word MVP

You can probably use

Set myMerge = ActiveDocument.MailMerge
If myMerge.State = wdMainAndDataSource Then myMerge.Execute

That comes from the Visual Basic Help File when F1 is pushed when the
selection is after the work MailMerge in the VBE.

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