2009-08-31 109 views
8

我有一个Excel数据表,并希望将其导出到一个新的Word文档。点击工作表上的按钮,是否可以从excel宏启动MAIL MERGE执行Word邮件合并

回答

16

如果您的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 
1

如果你的Word文档已经配置了数据源和合并字段布局,然后它变得更简单。在下面的示例中,MailMergeLayout.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 
4

要获得dendarii的解决方案来工作,我不得不在Excel VBA申报字常量,如下所示:

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

好一点,MattM。如果在Excel VBA中没有Word对象(在VBA编辑器,工具>参考> Microsoft Word [版本]对象库)中设置了引用,那么Word常量将不起作用,您将不得不使用MattM的值代替。 – dendarii 2013-02-27 09:53:08

0
Private Sub CommandButton1_Click() 

Set wordapp = CreateObject("word.Application") 

    wordapp.documents.Open "C:\Documents and Settings\User\Desktop\mergeletter.doc" 


    wordapp.Visible = True 

    wrddoc = wordapp.documents("C:\Users\User\Desktop\sourceofletters.xls") 


    wrddoc.mailmerge.maindocumenttype = wdformletters 

    With wrddoc.activedocument.mailmerge 

.OpenDataSource Name:="C:\Users\User\Desktop\sourceofletters.xls", _ 
      SQLStatement:="SELECT * FROM `Sheet1`" 



    End With 

End Sub 

上面的代码是打开一个Word邮件合并文档(其源链接,合并域代码中的所有设置),我要的是消息框"Opening the document will run the following SQL command "要提供给用户,从此时开始,用户既可以选择'Yes''No'

-1
Dim opt As String 
opt = MessageBox("Opening the document will run the following SQL command", vbYesNo) 
If opt = vbYes Then 
    'execute query 
End If 
+0

这甚至不太接近问题的解决方案。 – robnick 2017-02-28 00:28:43