2009-08-31 91 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