从Access 2013合并邮件,而不是Access 2000
有关于邮件合并和旧版本访问的文档:https://support.microsoft.com/en-us/kb/209976
但是我找不到2013年版本的任何文档或建议。
目的:在MS Access中有一个按钮,可以根据选定的数据触发邮件合并。
问题:
如果我手工操作,并在合并之前导出数据,则邮件合并工作得很好。如果我尝试创建一个活动链接,它会失败。我的表格是Office 365风格的共享点列表——这看起来会导致超时问题和锁定问题。所以我怀疑也许我需要在合并之前做一个导出。
Access 2000样式邮件合并代码:
Function MergeIt()
Dim objWord As Word.Document
Set objWord = GetObject("C:MyMerge.doc", "Word.Document")
' Make Word visible.
objWord.Application.Visible = True
' Set the mail merge data source as the Northwind database.
objWord.MailMerge.OpenDataSource _
Name:="C:Program FilesMicrosoft " & _
"OfficeOfficeSamplesNorthwind.mdb", _
LinkToSource:=True, _
Connection:="TABLE Customers", _
SQLStatement:="SELECT * FROM [Customers]"
' Execute the mail merge.
objWord.MailMerge.Execute
End Function
导出为xls,然后运行邮件合并。
Private Sub Command53_Click()
DoCmd.OutputTo acOutputQuery, "Mail Merge Query Name", acFormatXLS, "Temp export mail merge file.xls"
Set objWord = GetObject("path to Mail Merge file.docx", "Word.Document")
' objWord.Application.Visible = True
objWord.MailMerge.OpenDataSource _
Name:="Temp export mail merge file.xls", _
ReadOnly:=True, _
SQLStatement:="SELECT * FROM [Mail Merge Query Name$]"
objWord.MailMerge.Execute
objWord.Application.ActiveDocument.SaveAs2 FileName:="TmpMergeOuput.docx"
objWord.Application.ActiveDocument.Close
objWord.Close SaveChanges:=wdDoNotSaveChanges
End Sub
指出:
- 导出为XLS有效-但导出为TXT失败
- SQLStatement use [sheetname$] where表名是查询名。参见Call MailMerge.OpenDataSource() Set Connection
- 邮件合并嵌套项(子表)https://support.microsoft.com/en-us/kb/294686