You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VBA调用Word邮件合并发邮件失败:无法打开指定Word文件

Access VBA 邮件合并打开Word文档崩溃问题解决

我在Access中有一个数据表,已设置Word邮件合并并关联至该表,希望在表中指定日期向客户发送邮件。已创建Word模板,并通过分步邮件合并向导完成设置,准备通过VBA发送邮件。但在Access中运行VBA代码时程序持续崩溃,逐行注释排查后发现,问题出在执行Set wdDoc = wdApp.Documents.Open("C:\Users\Adam Khattab\Documents\Mail Merge - Copy.docx")这行代码(无法打开Word文件)。参考过Excel场景的相关方案,但不适用于Access,特此求助。

原代码如下:

Option Compare Database

Sub SendEmailsWord()

Const wdSendToEmail As Long = 0
Const wdMailFormatPlainText As Long = 2

On Error GoTo ErrorHandler

'Declare variables
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim strSQL As String
Dim rst As DAO.Recordset

'Set the reference to the Word application
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
On Error GoTo 0

If wdApp Is Nothing Then
  Set wdApp = CreateObject("Word.Application")
End If

'Open the mail merge document
Set wdDoc = wdApp.Documents.Open("C:\Users\Adam Khattab\Documents\Mail Merge - Copy.docx")

'Set the reference to the recordset
strSQL = "SELECT * FROM CustomerBookingTBL WHERE EmailAddress IS NOT NULL"
Set rst = CurrentDb.OpenRecordset(strSQL)

'Start the mail merge
wdDoc.MailMerge.OpenDataSource "C:\Users\Adam Khattab\Documents\Customer_Bookings_Backup.accdb", strSQL

'Loop through the recordset and send each email
Do Until rst.EOF
    wdDoc.MailMerge.Destination = wdSendToEmail
    wdDoc.MailMerge.SuppressBlankLines = True
    With wdDoc.MailMerge
        .MailFormat = wdMailFormatPlainText
        .MailSubject = "Mail Merge Subject"
        .MailAsAttachment = False
        .MailAddressFieldName = "EmailAddress"
        .Execute Pause:=False
    End With
    rst.MoveNext
Loop

'Close the mail merge document
wdDoc.Close False

'Close the Word application
wdApp.Quit

Exit Sub

ErrorHandler:
  MsgBox "An error occurred: " & Err.Description
End Sub

问题排查与解决方案

1. 文件路径与权限检查

  • 确认文件路径C:\Users\Adam Khattab\Documents\Mail Merge - Copy.docx完全正确:检查文件名拼写、空格或特殊字符,确保文件实际存在于该路径。
  • 验证Access的文件读取权限:右键文件→属性→安全,确认当前用户拥有读取权限;办公环境下可能需要联系管理员开放权限。
  • 手动打开该Word文档,确认文档未损坏、未被其他程序锁定(比如Word已打开该文档处于编辑状态)。

2. Word对象模型优化

  • 绑定方式调整:若使用早期绑定(Dim wdApp As Word.Application),需在VBA编辑器→工具→引用中勾选Microsoft Word xx.x Object Library(xx.x对应你的Word版本);若想避免版本兼容问题,可改用晚期绑定:将Dim wdApp As Word.Application改为Dim wdApp As Object,同时移除对应的引用。
  • 调试可见性设置:在创建/获取Word对象后添加wdApp.Visible = True,直观查看Word启动状态及文档打开情况,便于定位问题。

3. 邮件合并数据源修正

原代码中wdDoc.MailMerge.OpenDataSource参数调用错误,Access数据源需要明确指定连接字符串,且无需单独循环DAO记录集,邮件合并可直接处理筛选逻辑:

' 替换原OpenDataSource行
wdDoc.MailMerge.OpenDataSource _
    Name:="C:\Users\Adam Khattab\Documents\Customer_Bookings_Backup.accdb", _
    SQLStatement:="SELECT * FROM CustomerBookingTBL WHERE EmailAddress IS NOT NULL", _
    LinkToSource:=True, _
    Connection:="Provider=Microsoft.ACE.OLEDB.12.0;"

同时可删除原代码中rst相关的变量声明、SQL查询及循环逻辑,避免重复处理。

4. 错误处理增强

在打开文档的代码块前添加针对性错误捕获,精准定位问题:

On Error Resume Next
Set wdDoc = wdApp.Documents.Open("C:\Users\Adam Khattab\Documents\Mail Merge - Copy.docx")
If Err.Number <> 0 Then
    MsgBox "无法打开Word文档: " & Err.Description, vbCritical
    wdApp.Quit
    Set wdApp = Nothing
    Exit Sub
End If
On Error GoTo ErrorHandler

内容的提问来源于stack exchange,提问作者Adam Khattab

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 15:45:34