Word 2013 邮件合并 VBA "Argument not optional"

Word 2013 Mail Merge VBA "Argument not optional"

所以我创建了一个宏来允许我的用户打开文档 select a .csv,它会自动合并字母并打开一个新文档。我可以打开 VBA 和 运行 这个宏,但是,如果我尝试将它作为按钮添加到 QAT,则在单击它时会出现错误。 "Argument not optional"。这是编码,

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

您收到错误 Argument not optional,因为您在过程名称中使用了保留字 Merge

Sub Merge()改成Sub MyMailMerge()

注意:我没有检查其余代码,因为它超出了您的问题范围。不过我注意到一件事。

使用 Dialogs 时最好捕获任何错误。如果用户按下 取消 按钮会怎样?

Dim dlg As Word.Dialog

Set dlg = Dialogs(wdDialogMailMergeOpenDataSource)

If dlg.Show = 0 Then Exit Sub

'~~> Rest of your code