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

任务计划程序运行Excel VBA宏失败,创建Outlook对象报错

Excel VBA宏通过任务计划执行时创建Outlook对象失败问题

我编写了一个Excel VBA宏,通过WorkBook_Open()事件检查单元格数据并发送邮件,代码如下:

Option Explicit

Private Sub Workbook_Open()
        
        'Declaring variables
        Dim notifyEmailApplication As Object
        Dim notifyEmailContent As Object
        Dim triggerEmailApplication As Object
        Dim triggerEmailContent As Object

        'Create email object
        Set notifyEmailApplication = CreateObject("Outlook.Application")
        Set notifyEmailContent = notifyEmailApplication.CreateItem(0)
        Set triggerEmailApplication = CreateObject("Outlook.Application")
        Set triggerEmailContent = triggerEmailApplication.CreateItem(0)

        ...

随后编写了VBScript来运行该Excel文件:

Call ExcelMacro

Sub ExcelMacro() 

    Set xlApp = CreateObject("Excel.Application") 
    
    xlApp.Visible = True    
    xlApp.DisplayAlerts = False

    Set xlBook = xlApp.Workbooks.Open("....\Email Automation.xlsm", 0, False)       
    xlBook.Close    
    Set xlBook = Nothing
    
    xlApp.Quit

    Set xlApp = Nothing

End Sub 

同时创建了CMD文件,通过cscript.exe运行VBScript:

cscript.exe "....\vbscript.vbs"
exit

手动双击CMD文件时,宏可正常运行并发送邮件,但通过任务计划程序运行时,宏执行失败,报错行如下:

Set notifyEmailApplication = CreateObject("Outlook.Application")

已尝试以下修复方案但未解决:

  • 在dcomcnfg中设置Outlook Message Attachment为Interactive User
  • 将Dim notifyEmailApplication As Object改为Dim notifyEmailApplication As Outlook.Application,仍报错
  • 已在Excel VBA中添加Outlook Object Library引用

内容的提问来源于stack exchange,提问作者Xue Xian Heah

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 16:10:56