Access/Word 2010 VBA邮件合并尝试打开[Foldername] .mdb而不是ACCDB源 [英] Access / Word 2010 VBA Mail Merge Tries to Open [Foldername].mdb Instead of ACCDB Source

查看:273
本文介绍了Access/Word 2010 VBA邮件合并尝试打开[Foldername] .mdb而不是ACCDB源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试从Access内部自动进行邮件合并过程-单击按钮后,VBA将运行,该操作将当前数据库(一个accdb)指定为数据源,并根据以下代码运行SQL:

We are attempting to automate a mail merge process from within Access - upon clicking a button, VBA runs that specifies the current database (an accdb) as the data source and runs SQL, per the code below:

'Set up Word
Dim objWord As Object
Set objWord = CreateObject("Word.Application")

'Make visible, open specified doc to merge
With objWord
    .Visible = True
    .Documents.Open strDocName
End With

'Set the data source path
Dim docPath As String
docPath = CurrentProject.Path & "\" & CurrentProject.Name

'Open the merge data source - simplified
objWord.activedocument.mailmerge.opendatasource _
    Name:=docPath, _
    SQLStatement:=strSQL

strDocName和strSQL传入并包含有效的功能内容.

strDocName and strSQL are passed in and contain valid, functional contents.

成功打开单词,使其可见并打开要合并的模板.但是,此时要求确认数据源,并显示可能的数据源列表.单击全部显示"复选框后,我可以通过ODBC(.mdb,.accdb)向下滚动到MS Access数据库,然后从列表中选择它.

It succeeds in opening word, making it visible and opening the template to merge into. At that point, however, it comes up asking to confirm data source, and shows a list of possible data sources. Upon clicking the 'show all' checkbox, I can scroll down to MS Access Database via ODBC (.mdb, .accdb) and select it from the list.

然后显示一个标题为"ODBC Microsoft Access驱动程序登录失败"的框,提示找不到文件:"[数据库文件夹的路径]/[包含文件夹的名称] .mdb""

It then presents with a box titled 'ODBC Microsoft Access Driver Login Failed', saying 'Could Not Find File: "[path to database folder]/[name of containing folder].mdb"'

因此,如果我的数据库位于C:\ Temp,则错误路径将显示为'C:\ Temp.mdb'.将Access数据库移动到另一个文件夹会导致错误路径更新,以根据包含的文件夹查找相应命名的mdb文件.

So, if my database were located in C:\Temp the error path would read 'C:\Temp.mdb'. Moving the access database to another folder causes the error path to update looking for an accordingly named mdb file based on the containing folder.

我不知道它是否相关,但它似乎也在以编辑模板本身的方式打开模板,而不是从该模板生成新文档.

I don't know if it's relevant but it also appears to be opening the template in the mode for editing the template itself, rather than generating a new document from that template.

我在这里错过了什么吗?还是有人有什么想法?

Am I missing something here or does anyone have any ideas?

欢呼

在下面的Remou的帮助下,我们放弃了直接与Access的链接,并使用了Remou的建议链接中的代码输出到一个临时文本文件,然后从那里合并.

With the assistance of Remou below, we gave up linking directly to Access and used the code from Remou's suggested link to output to a temporary text file, then merge from there.

我们修改了代码以删除CurrentBackendPath(),并更改了对该函数的唯一引用,如下所示:

We modified the code to remove CurrentBackendPath() and changed the only reference to this function as follows:

Private Function GetStartDirectory() As String
    'GetStartDirectory = CurrentBackendPath() & "mm\"
    GetStartDirectory = CurrentProject.Path & "\mm\"
End Function

我们使用的唯一其他修改是将代码更改为特定于我们的目的.再次感谢大家的答复.如果我有足够的声誉,我会赞扬Remou的回应!

The only other modifications we used were to change the code to be specific to our purpose. Thanks once again to everyone for your replies. I would upvote Remou for their response if I had enough reputation to do so!

推荐答案

我强烈建议您链接到MS Access文件.将所需的数据输出到文本文件并链接到该文件.对于手动控制的邮件合并,链接到Access都非常好,但是当您希望实现自动化时,它变得非常繁琐.您可能希望阅读 http://www.tek-tips.com/faqs. cfm?fid = 5088

I would strongly advise you not to link to an MS Access file. Output the data required to a text file and link to that. Linking to Access is all very well for manually controlled mailmerges, but it becomes tedious in the extreme when you wish to automate. You may wish to read http://www.tek-tips.com/faqs.cfm?fid=5088

话虽如此,记录一个宏,我得到以下信息:

That being said, recording a macro, I get the following:

ActiveDocument.MailMerge.OpenDataSource Name:="Z:\Docs\Test.accdb", _
    ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
    AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
    WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
    Format:=wdOpenFormatAuto, Connection:= _
    "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=Z:\Docs\Test.accdb;Mode=Read;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk" _
    , SQLStatement:="SELECT * FROM `Table1`", SQLStatement1:="", SubType:= _
    wdMergeSubTypeAccess

上面的大多数内容都是不必要的,但是您将看到它包含一个连接字符串.在早期版本中不是这种情况.您可以通过以下方式获得帮助:

Most of the above is unnecessary, but you will see that it includes a connection string. This was not the case in earlier versions. You can get by with:

With objWord.ActiveDocument.MailMerge
    .OpenDataSource Name:="Z:\Docs\Test.accdb", _
        ConfirmConversions:=False, LinkToSource:=True, _
        Connection:= _
        "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Z:\Docs\Test.accdb;" _
        , SQLStatement:="SELECT * FROM `Table1`"
End With

这篇关于Access/Word 2010 VBA邮件合并尝试打开[Foldername] .mdb而不是ACCDB源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆