Access / Word 2010 VBA 邮件合并尝试打开 [Foldername].mdb 而不是 ACCDB 源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13171891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:18:49  来源:igfitidea点击:

Access / Word 2010 VBA Mail Merge Tries to Open [Foldername].mdb Instead of ACCDB Source

vbams-accessodbcmailmerge

提问by WaideWalker

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:

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

'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 and strSQL are passed in and contain valid, functional contents.

strDocName 和 strSQL 被传入并包含有效的功能性内容。

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(.mdb、.accdb)向下滚动到 MS Access 数据库并从列表中选择它。

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"'

然后它会显示一个标题为“ODBC Microsoft 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.

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

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?

我在这里遗漏了什么还是有人有任何想法?

Cheers

干杯



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.

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

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

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

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

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!

我们使用的唯一其他修改是根据我们的目的更改代码。再次感谢大家的回复。如果我有足够的声誉这样做,我会赞成 Remou 的回应!

采纳答案by Fionnuala

I would strongly advise you notto 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

我强烈建议您不要链接到 MS Access 文件。将所需的数据输出到文本文件并链接到该文件。链接到 Access 非常适合手动控制的邮件合并,但是当您希望自动化时,它变得非常乏味。您不妨阅读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