Thread: [Solved] Mail Merge Macro
View Single Post
 
Old 06-05-2015, 05:31 AM
gmayor's Avatar
gmayor gmayor is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,142
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The problem is the macro name which conflicts with Word built-in functions, call it e.g. MyMerge and you should be OK, though your error handling is a little off - try

Code:
Sub MyMerge()
    On Error GoTo err_Handler
    If Not ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument Then
        Dialogs(wdDialogMailMergeOpenDataSource).Show
        With ActiveDocument.MailMerge
            .Destination = wdSendToNewDocument
            .SuppressBlankLines = True
            With .DataSource
                .FirstRecord = wdDefaultFirstRecord
                .LastRecord = wdDefaultLastRecord
            End With
            .Execute Pause:=False
        End With
        Documents("C:\Documents\mytemplate.docm").Close SaveChanges:=wdDoNotSaveChanges
    Else
        MsgBox "Not a merge document"
    End If
lbl_Exit:
    Exit Sub
err_Handler:
    If Err.Number = 4160 Then
        MsgBox "The file specified is not open.", vbCritical Or vbOKOnly, _
               "File Not Open"
    End If
    GoTo lbl_Exit
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote