我正在开发一个Access数据库,该数据库会生成一些邮件合并的邮件,这些邮件是从Access数据库中的VBA代码调用的。问题是,如果我打开一个新的Word文档并启动邮件合并(VBA),Word将打开相同的Access数据库(已打开)以获取数据。有什么方法可以防止这种情况吗?那么使用已打开的数据库实例?
经过一些测试后,我得到了一个奇怪的行为:如果我打开存在SHIFT-Key的Access数据库,则邮件合并不会打开同一数据库的其他Access实例。如果我在没有按住键的情况下打开Access数据库,我会得到描述的行为。
我的邮件合并VBA代码:
On Error GoTo ErrorHandler
Dim word As word.Application
Dim Form As word.Document
Set word = CreateObject("Word.Application")
Set Form = word.Documents.Open("tpl.doc")
With word
word.Visible = True
With .ActiveDocument.MailMerge
.MainDocumentType = wdMailingLabels
.OpenDataSource Name:= CurrentProject.FullName, ConfirmConversions:=False, _
ReadOnly:=False, LinkToSource:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
SQLStatement:="[MY QUERY]", _
SQLStatement1:="", _
SubType:=wdMergeSubTypeWord2000, OpenExclusive:=False
.Destination = wdSendToNewDocument
.Execute
.MainDocumentType = wdNotAMergeDocument
End With
End With
Form.Close False
Set Form = Nothing
Set word = Nothing
Exit_Error:
Exit Sub
ErrorHandler:
word.Quit (False)
Set word = Nothing
' ...
End Sub
使用Access / Word 2003完成整个过程。
更新#1 如果有人能告诉我在使用或不使用SHIFT-Key打开Access之间的确切区别,这也会有所帮助。如果可以编写一些VBA代码来启用“功能”,那么如果在没有SHIFT-Key的情况下打开数据库,它至少会“模拟”它。
干杯, 格里
答案 0 :(得分:8)
当我使用mailmerges时,我通常从Access导出.txt文件,然后将邮件合并数据源设置为该文件。这样,Access只涉及导出查询,然后告诉Word文档通过自动化完成工作,大致如下:
Public Function MailMergeLetters()
Dim pathMergeTemplate As String
Dim sql As String
Dim sqlWhere As String
Dim sqlOrderBy As String
'Get the word template from the Letters folder
pathMergeTemplate = "C:\MyApp\Resources\Letters\"
'This is a sort of "base" query that holds all the mailmerge fields
'Ie, it defines what fields will be merged.
sql = "SELECT * FROM MailMergeExportQry"
With Forms("MyContactsForm")
' Filter and order the records you want
'Very much to do for you
sqlWhere = GetWhereClause()
sqlOrderBy = GetOrderByClause()
End With
' Build the sql string you will use with this mail merge
sql = sql & sqlWhere & sqlOrderBy & ";"
'Create a temporary QueryDef to hold the query
Dim qd As DAO.QueryDef
Set qd = New DAO.QueryDef
qd.sql = sql
qd.Name = "mmexport"
CurrentDb.QueryDefs.Append qd
' Export the data using TransferText
DoCmd.TransferText _
acExportDelim, , _
"mmexport", _
pathMergeTemplate & "qryMailMerge.txt", _
True
' Clear up
CurrentDb.QueryDefs.Delete "mmexport"
qd.Close
Set qd = Nothing
'------------------------------------------------------------------------------
'End Code Block:
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
'Start Code Block:
'OK. Access has built the .txt file.
'Now the Mail merge doc gets opened...
'------------------------------------------------------------------------------
Dim appWord As Object
Dim docWord As Object
Set appWord = CreateObject("Word.Application")
appWord.Application.Visible = True
' Open the template in the Resources\Letters folder:
Set docWord = appWord.Documents.Add(Template:=pathMergeTemplate & "MergeLetters.dot")
'Now I can mail merge without involving currentproject of my Access app
docWord.MailMerge.OpenDataSource Name:=pathMergeTemplate & "qryMailMerge.txt", LinkToSource:=False
Set docWord = Nothing
Set appWord = Nothing
'------------------------------------------------------------------------------
'End Code Block:
'------------------------------------------------------------------------------
Finally:
Exit Function
Hell:
MsgBox Err.Description & " " & Err.Number, vbExclamation, APPHELP
On Error Resume Next
CurrentDb.QueryDefs.Delete "mmexport"
qd.Close
Set qd = Nothing
Set docWord = Nothing
Set appWord = Nothing
Resume Finally
End Function
要使用此功能,您需要设置Resources \ Letters子文件夹并将mailmerge模板文件放在那里。您还需要使用Access应用程序中的字段定义进行“基本”查询(在示例中,它称为MailMergeExportQry。但您可以将其称为任何内容。
您还需要确定要执行的过滤和排序。在示例中,这由
表示sqlWhere = GetWhereClause()
sqlOrderBy = GetOrderByClause
一旦你掌握了这些东西,这是非常可重复使用的。