我正在开发一个Access数据库,该数据库通过从Access数据库中的VBA代码调用邮件合并来生成一些邮件。问题是,如果我打开一个新的Word文档并启动邮件合并(VBA),Word会打开同一个Access数据库(已打开)来获取数据。有什么办法可以防止这种情况发生吗?是否使用已打开的数据库实例?
经过一些测试,我得到了一个奇怪的行为:如果我打开持有SHIFT键的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/Word2003完成的。
Update #1 It would also help if someone could tell me what the exact difference is between opening Access with or without the SHIFT-Key. And if it is possible to write some VBA code to enable the "features" so if the database is opened without the SHIFT-Key, it at least "simulates" it.
Cheers, Gregor