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

调用MailMerge.OpenDataSource时出现VBA错误4605:‘宏正在运行’

Word邮件合并宏报错4605(“宏正在运行”)解决方案

问题背景

通过Excel存储客户送货单数据,Excel端VBA宏启动Word并调用Word内的宏执行邮件合并生成可打印文档,该方案正常运行一年后,两台Windows机器同时出现错误4605,报错点为MailMerge.OpenDataSource语句,提示原因“宏正在运行”。已尝试修改参数、添加延迟、暂停Excel宏等操作,均未解决。

相关Word宏代码:

Public Sub PopulatePrintExit(odbcFile As String, pdfFile As String)

    On Error GoTo closeThis
    
   With ThisDocument.MailMerge
        .OpenDataSource Name:= _
            odbcFile, ConfirmConversions:=False, _
            ReadOnly:=True, LinkToSource:=True, AddToRecentFiles:=False, _
            Revert:=False, Format:=wdOpenFormatAuto, _
            Connection:= _
            "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=OdbcFile;Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Typ", _
            SQLStatement:="SELECT * FROM `Fraktsedelsdata$` WHERE (`Aviseringsnr` IS NOT null And `Aviseringsnr` <> '')"
            
        .Destination = wdSendToNewDocument
        On Error GoTo closeAll
        .Execute
    End With
...

解决步骤

1. 修复Connection字符串拼写错误

原代码中Jet OLEDB:Engine Typ缺少末尾字母e,且Data Source=OdbcFile未使用传入的变量,修正后:

.Connection:= _
    "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=" & odbcFile & ";Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=37"

拼写错误可能导致数据源连接异常,触发后续的“宏正在运行”错误提示。

2. 重置Word邮件合并状态

在调用OpenDataSource前,强制重置邮件合并的文档类型,清除可能残留的运行状态:

With ThisDocument.MailMerge
    .MainDocumentType = wdNotAMergeDocument ' 重置为非合并文档
    .MainDocumentType = wdFormLetters ' 恢复为需要的合并文档类型
    ' 后续执行OpenDataSource
    .OpenDataSource ...
End With

3. 调整跨进程调用逻辑

放弃Excel调用Word宏的方式,直接在Excel VBA中控制Word对象完成邮件合并,避免跨进程宏的互斥冲突:

' Excel端VBA代码示例
Sub RunMailMergeFromExcel()
    Dim wdApp As Object, wdDoc As Object
    Dim odbcFile As String, pdfFile As String
    
    odbcFile = "你的Excel数据文件路径"
    pdfFile = "输出PDF路径"
    
    Set wdApp = CreateObject("Word.Application")
    wdApp.Visible = True ' 调试时可见,发布后可设为False
    Set wdDoc = wdApp.Documents.Open("你的Word模板路径")
    
    With wdDoc.MailMerge
        .MainDocumentType = wdFormLetters
        .OpenDataSource Name:=odbcFile, _
            ConfirmConversions:=False, ReadOnly:=True, LinkToSource:=True, _
            AddToRecentFiles:=False, Revert:=False, Format:=wdOpenFormatAuto, _
            Connection:= _
            "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=" & odbcFile & ";Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=37", _
            SQLStatement:="SELECT * FROM `Fraktsedelsdata$` WHERE (`Aviseringsnr` IS NOT null And `Aviseringsnr` <> '')"
        .Destination = wdSendToNewDocument
        .Execute
    End With
    
    ' 后续处理生成的文档,比如保存为PDF
    wdApp.ActiveDocument.SaveAs2 pdfFile, wdFormatPDF
    wdApp.ActiveDocument.Close False
    wdDoc.Close False
    wdApp.Quit
    Set wdDoc = Nothing
    Set wdApp = Nothing
End Sub

4. 检查Office更新与回滚

错误突然出现大概率是Office自动更新引发的兼容性问题:

  • 查看两台机器的Office版本,确认更新时间是否与报错时间吻合
  • 尝试回滚到之前正常运行的Office版本,或安装最新的Office补丁修复已知bug

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 15:15:33