Access 中的邮件合并 - 保存合并的文档

发布于 2024-09-12 07:37:04 字数 1198 浏览 7 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜点 2024-09-19 07:37:04

将 ActiveDocument 更改为 objWord.ActiveDocument。最终得到了想要的结果。

谢谢雷穆。

Changed ActiveDocument to objWord.ActiveDocument. Ended up with the desired results.

Thanks Remou.

给妤﹃绝世温柔 2024-09-19 07:37:04

我刚刚经历过这个。这就是我正在做的事情并且效果很好。 o文档是用户通过打开的对话框选择的合并表单。 Excel 文件是我之前导出并保存在用户临时文件夹中的查询。我在 Access 查询和临时表中尝试了这种技术,但发现使用 excel 更轻松。

Sleep 命令来自导入的系统 dll 函数(Public Declare Sub Sleep Lib "kernel32" (ByVal dwMS As Long)),并为 Word 提供运行合并的时间。事实上,这可能就是您所需要的。这是使用 Office 2007。

If Not oDocument Is Nothing Then
                  ' get merge source file
               Set oFSO = New FileSystemObject
               Set oFolder = oFSO.GetSpecialFolder(TemporaryFolder)
               strTempFile = oFolder.Path & "\PTDMergeSource.xls"

                  ' run merge
               With oDocument.MailMerge
                   .MainDocumentType = wdFormLetters
                   .Destination = wdSendToNewDocument
                   .OpenDataSource strTempFile, WdOpenFormat.wdOpenFormatDocument, False, False, False, False, , , , , , , "SELECT * FROM `tblMerge

我刚刚经历过这个。这就是我正在做的事情并且效果很好。 o文档是用户通过打开的对话框选择的合并表单。 Excel 文件是我之前导出并保存在用户临时文件夹中的查询。我在 Access 查询和临时表中尝试了这种技术,但发现使用 excel 更轻松。

Sleep 命令来自导入的系统 dll 函数(Public Declare Sub Sleep Lib "kernel32" (ByVal dwMS As Long)),并为 Word 提供运行合并的时间。事实上,这可能就是您所需要的。这是使用 Office 2007。

", , False, WdMergeSubType.wdMergeSubTypeAccess .Execute True End With Sleep 2 oDocument.Close False Else MsgBox "Action was cancelled, or there was an error opening that document. Please try again, then try opening that document in Word. It may be someone else has locked that document (they are editing it). If the problem persists, email the document to the support contractor." End If

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.

If Not oDocument Is Nothing Then
                  ' get merge source file
               Set oFSO = New FileSystemObject
               Set oFolder = oFSO.GetSpecialFolder(TemporaryFolder)
               strTempFile = oFolder.Path & "\PTDMergeSource.xls"

                  ' run merge
               With oDocument.MailMerge
                   .MainDocumentType = wdFormLetters
                   .Destination = wdSendToNewDocument
                   .OpenDataSource strTempFile, WdOpenFormat.wdOpenFormatDocument, False, False, False, False, , , , , , , "SELECT * FROM `tblMerge

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.

", , False, WdMergeSubType.wdMergeSubTypeAccess .Execute True End With Sleep 2 oDocument.Close False Else MsgBox "Action was cancelled, or there was an error opening that document. Please try again, then try opening that document in Word. It may be someone else has locked that document (they are editing it). If the problem persists, email the document to the support contractor." End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文