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



所以我创建了一个宏来允许我的用户打开一个文档选择一个。csv,它会自动合并字母并打开一个新文档。我可以打开VBA并运行这个宏,但是,如果我尝试将此添加到QAT作为一个按钮,它会在点击它时给我一个错误。"论证不可选"。这是编码,

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:UsersuserDocumentsmytemplate.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

相关内容

最新更新