Access 中的邮件合并 - 保存合并的文档
我正在尝试从 Access 打开文档,执行邮件合并,然后使用 VBA 保存合并的文档输出。
这是我当前的尝试:
Dim templateName as String, tempRoot as String
tempRoot = "C:\report\"
templateName = tempRoot & "template.doc"
Dim objDoc As Word.Document
Dim objWord As New Word.Application
Set objDoc = objWord.Documents.Open(templateName)
objWord.Visible = True
exportData "AnnualData", tempRoot & "annualData.txt" 'Outputs query to txt file for merge
objDoc.MailMerge.OpenDataSource NAME:= _
tempRoot & "annualData.txt", ConfirmConversions:=False, ReadOnly _
:=False, LinkToSource:=True, AddToRecentFiles:=False, PasswordDocument:= _
"", PasswordTemplate:="", WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
Connection:="", SQLStatement:="", SQLStatement1:="", SubType:= _
wdMergeSubTypeOther
objDoc.MailMerge.Execute
objDoc.Close False 'Ideally after closing, the new document becomes the active document?
ActiveDocument.SaveAs tempRoot & "testReport.doc" 'And then save?
Set objWord = Nothing
Set objDoc = Nothing
我得到了合并的文档,但是我无法保存它。我收到一条错误消息,指出在没有打开文档的情况下无法执行该命令。
如果有人可以提供任何建议,我们将不胜感激。
I am attempting to open a document from access, execute a mail merge, and then save the document output from the merge using VBA.
Here is my current attempt:
Dim templateName as String, tempRoot as String
tempRoot = "C:\report\"
templateName = tempRoot & "template.doc"
Dim objDoc As Word.Document
Dim objWord As New Word.Application
Set objDoc = objWord.Documents.Open(templateName)
objWord.Visible = True
exportData "AnnualData", tempRoot & "annualData.txt" 'Outputs query to txt file for merge
objDoc.MailMerge.OpenDataSource NAME:= _
tempRoot & "annualData.txt", ConfirmConversions:=False, ReadOnly _
:=False, LinkToSource:=True, AddToRecentFiles:=False, PasswordDocument:= _
"", PasswordTemplate:="", WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
Connection:="", SQLStatement:="", SQLStatement1:="", SubType:= _
wdMergeSubTypeOther
objDoc.MailMerge.Execute
objDoc.Close False 'Ideally after closing, the new document becomes the active document?
ActiveDocument.SaveAs tempRoot & "testReport.doc" 'And then save?
Set objWord = Nothing
Set objDoc = Nothing
I get the merged document, however, I am unable to save it. I receive an error stating that the command cannot be performed when no document is open.
If anyone can provide any suggestions, it would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 ActiveDocument 更改为 objWord.ActiveDocument。最终得到了想要的结果。
谢谢雷穆。
Changed ActiveDocument to objWord.ActiveDocument. Ended up with the desired results.
Thanks Remou.
我刚刚经历过这个。这就是我正在做的事情并且效果很好。 o文档是用户通过打开的对话框选择的合并表单。 Excel 文件是我之前导出并保存在用户临时文件夹中的查询。我在 Access 查询和临时表中尝试了这种技术,但发现使用 excel 更轻松。
Sleep 命令来自导入的系统 dll 函数(Public Declare Sub Sleep Lib "kernel32" (ByVal dwMS As Long)),并为 Word 提供运行合并的时间。事实上,这可能就是您所需要的。这是使用 Office 2007。
I just went through this. Here's what I'm doing and it works well. oDocument is the merge form that the user selects via an open dialog box. The excel file is the query that I've previously exported and stuck in the users temp folder. I tried this technique with Access queries and temp tables, but found that using excel was much more trouble free.
The Sleep command is from an imported system dll function ( Public Declare Sub Sleep Lib "kernel32" (ByVal dwMS As Long) ) and gives Word time to run the merge. Actually, that may be all you need. This is using Office 2007.