Access 中 VBA 启动的邮件合并让 Word 再次打开数据库
我正在开发一个 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 / Word 2003 完成的。
更新#1 如果有人能告诉我使用或不使用 SHIFT 键打开 Access 的确切区别是什么,也会有所帮助。如果可以编写一些 VBA 代码来启用“功能”,那么如果在没有 SHIFT 键的情况下打开数据库,它至少会“模拟”它。
干杯, 格雷戈尔
I'm working on a Access database which generates some mails with mail merge called from VBA code in the Access database. The problem is that if I open a new Word document and start the mail merge (VBA), Word opens the same Access database (which is already open) to get the data. Is there any way to prevent this? So that the already opened instance of the database is used?
After some testing I get a strange behavior: If I open the Access database holding the SHIFT-Key the mail merge does not open an other Access instance of the same database. If I open the Access database without holding the key, I get the described behavior.
My mail merge VBA code:
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
The whole thing is done with Access / Word 2003.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我进行邮件合并时,我通常从 Access 导出 .txt 文件,然后将邮件合并数据源设置为该文件。这样,Access 只涉及导出查询,然后告诉 Word 文档通过自动化完成工作,大致如下:
要使用此功能,您需要设置 Resources\Letters 子文件夹并将邮件合并模板 Word 文件放在那里。您还需要使用 Access 应用程序中的字段定义进行“基本”查询(在示例中,它称为 MailMergeExportQry。但您可以将其称为任何名称。
您还需要弄清楚将执行哪些过滤和排序。在示例中,这表示为
一旦你了解了这些事情,这是高度可重用的。
When I do mailmerges, I usually export a .txt file from Access and then set the mail merge datasource to that. That way Access is only involved in exporting the query and then telling the Word document to do the work via automation, roughly as follows:
To use this, you need to set up your Resources\Letters subfolder and put your mailmerge template word file in there. You also need your "base" query with the field definitions in your Access App (in the example, it is called MailMergeExportQry. But you can call it anything.
You also need to figure out what filtering and sorting you will do. In the example, this is represented by
Once you have got your head round those things, this is highly reusable.