ch1325
ch1325

Reputation: 21

Word 2013 Mail Merge VBA "Argument not optional"

So I created a macro to allow my users to open a document select a .csv and it automatically merges the letters and opens a new document. I can open VBA and run this macro okay however, if I try adding this to the QAT as a button it gives me an error when clicking on it. "Argument not optional". This is the coding,

Sub Merge()
    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:\Users\user\Documents\mytemplate.docm").Close
    SaveChanges:=wdDoNotSaveChanges
    If Err.Number = 4160 Then
        MsgBox "The file specified is not open.", vbCritical Or vbOKOnly, _
        "File Not Open"
    End If
    On Error GoTo 0
End Sub

Upvotes: 1

Views: 145

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149295

You are getting the error Argument not optional because you are using a Reserved word Merge for the Procedure Name.

Change Sub Merge() to say Sub MyMailMerge()

Note: I have not checked the rest of the code as it is out of the scope of your question. However I noticed one thing.

When using Dialogs it is better to trap any errors. What if the user presses the Cancel Button?

Dim dlg As Word.Dialog

Set dlg = Dialogs(wdDialogMailMergeOpenDataSource)

If dlg.Show = 0 Then Exit Sub

'~~> Rest of your code

Upvotes: 1

Related Questions