Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 执行Word邮件合并_Excel_Vba_Ms Word - Fatal编程技术网

Excel 执行Word邮件合并

Excel 执行Word邮件合并,excel,vba,ms-word,Excel,Vba,Ms Word,我有一个包含数据的excel工作表,希望将其导出到新的word文档中 是否可以通过单击工作表上的按钮从excel宏启动邮件合并?如果您的Word文档已经配置了合并字段,并且您正在从包含要合并到Word文档中的数据的工作簿运行宏,请尝试以下操作: Sub RunMerge() Dim wd As Object Dim wdocSource As Object Dim strWorkbookName As String On Error Resume Next

我有一个包含数据的excel工作表,希望将其导出到新的word文档中


是否可以通过单击工作表上的按钮从excel宏启动
邮件合并

如果您的Word文档已经配置了合并字段,并且您正在从包含要合并到Word文档中的数据的工作簿运行宏,请尝试以下操作:

Sub RunMerge()

    Dim wd As Object
    Dim wdocSource As Object

    Dim strWorkbookName As String

    On Error Resume Next
    Set wd = GetObject(, "Word.Application")
    If wd Is Nothing Then
        Set wd = CreateObject("Word.Application")
    End If
    On Error GoTo 0

    Set wdocSource = wd.Documents.Open("c:\test\WordMerge.docx")

    strWorkbookName = ThisWorkbook.Path & "\" & ThisWorkbook.Name

    wdocSource.MailMerge.MainDocumentType = wdFormLetters

    wdocSource.MailMerge.OpenDataSource _
            Name:=strWorkbookName, _
            AddToRecentFiles:=False, _
            Revert:=False, _
            Format:=wdOpenFormatAuto, _
            Connection:="Data Source=" & strWorkbookName & ";Mode=Read", _
            SQLStatement:="SELECT * FROM `Sheet1$`"

    With wdocSource.MailMerge
        .Destination = wdSendToNewDocument
        .SuppressBlankLines = True
        With .DataSource
            .FirstRecord = wdDefaultFirstRecord
            .LastRecord = wdDefaultLastRecord
        End With
        .Execute Pause:=False
    End With

    wd.Visible = True
    wdocSource.Close SaveChanges:=False

    Set wdocSource = Nothing
    Set wd = Nothing

End Sub

如果您的word文档已经配置了数据源和合并字段布局,那么它会变得更简单。在下面的示例中,mailmergeloayout.doc已准备好执行合并。Excel中的一个按钮链接到RunMailMerge(),如下所示。所有代码都包含在Excel VBA模块中

Sub RunMailMerge()

    Dim wdOutputName, wdInputName As String
    wdOutputName = ThisWorkbook.Path & "\Reminder Letters " & Format(Date, "d mmm yyyy")
    wdInputName = ThisWorkbook.Path & "\MailMergeLayout.doc"

    ' open the mail merge layout file
    Dim wdDoc As Object
    Set wdDoc = GetObject(wdInputName, "Word.document")
    wdDoc.Application.Visible = True

    With wdDoc.MailMerge
         .MainDocumentType = wdFormLetters
         .Destination = wdSendToNewDocument
         .SuppressBlankLines = True
         .Execute Pause:=False
    End With

    ' show and save output file
    wdDoc.Application.Visible = True
    wdDoc.Application.ActiveDocument.SaveAs wdOutputName

    ' cleanup
    wdDoc.Close SaveChanges:=False
    Set wdDoc = Nothing

End Sub

为了让dendarii的解决方案发挥作用,我必须在Excel VBA中声明Word常量,如下所示:

' Word constants
Const wdFormLetters = 0, wdOpenFormatAuto = 0
Const wdSendToNewDocument = 0, wdDefaultFirstRecord = 1, wdDefaultLastRecord = -16

上面的代码是打开一个word mailmerge文档(带有其源链接和mergefield codes all设置),我想要的只是消息框
“打开文档将运行以下SQL命令”
,用户可以从这一点开始选择
“是”
“否”

说得好,马特姆。如果您没有在Excel VBA中设置对Word对象的引用(在VBA编辑器中,工具>引用>Microsoft Word[version]对象库中),Word常量将不起作用,您将不得不使用MattM的值。这甚至远未接近问题的解决方案。
Dim opt As String
opt = MessageBox("Opening the document will run the following SQL command", vbYesNo)
If opt = vbYes Then
   'execute query
End If
Dim opt As String
opt = MessageBox("Opening the document will run the following SQL command", vbYesNo)
If opt = vbYes Then
   'execute query
End If