【发布时间】:2009-08-31 11:59:04
【问题描述】:
我有一个包含数据的 Excel 表,想将其导出到新的 Word 文档中。
是否可以通过单击工作表上的按钮从 excel 宏启动 MAIL MERGE?
【问题讨论】:
我有一个包含数据的 Excel 表,想将其导出到新的 Word 文档中。
是否可以通过单击工作表上的按钮从 excel 宏启动 MAIL MERGE?
【问题讨论】:
如果您的 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
【讨论】:
为了让 dendarii 的解决方案发挥作用,我必须在 Excel VBA 中声明 Word 常量,如下所示:
' Word constants
Const wdFormLetters = 0, wdOpenFormatAuto = 0
Const wdSendToNewDocument = 0, wdDefaultFirstRecord = 1, wdDefaultLastRecord = -16
【讨论】:
如果您的 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
【讨论】:
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'。
【讨论】:
Dim opt As String
opt = MessageBox("Opening the document will run the following SQL command", vbYesNo)
If opt = vbYes Then
'execute query
End If
【讨论】: