【问题标题】:Excel VBA code throws run-time error 429 while sending Outlook email using Task SchedulerExcel VBA 代码在使用任务计划程序发送 Outlook 电子邮件时引发运行时错误 429
【发布时间】:2018-03-08 16:45:07
【问题描述】:

我正在使用 Excel VBA 宏在每天指定的时间发送自动电子邮件 (Outlook 2013),该电子邮件与 Windows 任务计划程序一起运行(我正在使用批处理文件来执行此操作)。当我在没有任务计划程序的情况下运行宏时,它会正常执行(发送电子邮件),但是当我使用任务计划程序时,我收到“运行时错误 429”,这仅在 VBA 宏尝试创建 Outlook 对象时发生:

Dim OutApp As Object
Dim OutMail As Object

Set OutApp = CreateObject("Outlook.Application") 'The error happens here
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
    .to = "email@email.com"
    .CC = ""
    .BCC = ""
    .Subject = "subj"
    .Body = "body"
    .Attachments.Add ActiveWorkbook.FullName
    .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

只有在计算机上打开 Outlook 应用程序时才会发生上述错误。 现在我不明白的是:

  1. 为什么宏在没有任务计划程序的情况下可以正常工作(不管 Outlook 是否打开),为什么它不能在那里工作?

  2. 如何使用任务计划程序执行整个流程,而不依赖于打开或关闭 Outlook 应用程序? (即,无论打开/关闭哪些应用程序,我都希望宏运行)。

建议将不胜感激。

编辑:这是我用来执行宏的 VBScript 代码(以回应 LS_ᴅᴇᴠ 的问题):

    Dim WshShell
Set WshShell = CreateObject("WScript.Shell")

' Create an Excel instance
Dim myExcelWorker
Set myExcelWorker = CreateObject("Excel.Application") 

' Disable Excel UI elements
myExcelWorker.DisplayAlerts = False
myExcelWorker.AskToUpdateLinks = False
myExcelWorker.AlertBeforeOverwriting = False
myExcelWorker.FeatureInstall = msoFeatureInstallNone

' Tell Excel what the current working directory is 
' (otherwise it can't find the files)
Dim strSaveDefaultPath
Dim strPath
strSaveDefaultPath = myExcelWorker.DefaultFilePath
strPath = WshShell.CurrentDirectory
myExcelWorker.DefaultFilePath = strPath

' Open the Workbook specified on the command-line 
Dim oWorkBook
Dim strWorkerWB
strWorkerWB = strPath & "\____DailyReport.xlsm"

Set oWorkBook = myExcelWorker.Workbooks.Open(strWorkerWB)

' Build the macro name with the full path to the workbook
Dim strMacroName
strMacroName = "'" & strPath & "\____DailyReport.xlsm'" & "!Module1.____DailyRep"
on error resume next 
   ' Run the calculation macro
   myExcelWorker.Run strMacroName
   if err.number <> 0 Then
      ' Error occurred - just close it down.
   End If
   err.clear
on error goto 0 

'oWorkBook.Save 
'oWorkBook.Close <<--- we don't need these two because we close the WB in the VBA macro

myExcelWorker.DefaultFilePath = strSaveDefaultPath

' Clean up and shut down
Set oWorkBook = Nothing

' Don’t Quit() Excel if there are other Excel instances 
' running, Quit() will 
'shut those down also
if myExcelWorker.Workbooks.Count = 0 Then
   myExcelWorker.Quit
End If

Set myExcelWorker = Nothing
Set WshShell = Nothing

【问题讨论】:

  • 这是一个 Excel 宏,你在任务调度器中放了什么批次?
  • @LS_ᴅᴇᴠ 我使用的是此处描述的 VBScript:Excel: Running Excel on Windows Task Scheduler,我的批处理文件如下所示:start "" "D:\Documents\Macros\___\Run__Rep.vbs"
  • 那么,另一个问题... VBS包含什么?
  • @LS_ᴅᴇᴠ 我已经编辑了帖子(添加了 VBS 代码)。

标签: excel vba outlook


【解决方案1】:

导致错误的原因是我试图“以最高权限”运行任务:

这在我的环境中显然不可行,所以当我取消选中它时,我正在使用的 VBScript 和@Nikolaos Polygenis 建议的 VBScript 都正常执行。

【讨论】:

    【解决方案2】:

    您应该首先检查 Outlook 是否正在运行,如果是,请附加到它而不是创建新会话:

    On Error Resume Next
    Set objOutlook = GetObject(, "Outlook.Application")    'Error if Outlook not running
    On Error GoTo 0
    If objOutlook Is Nothing Then  'Outlook not running so start it
        Set objOutlook = CreateObject("Outlook.Application")
    End If
    

    【讨论】:

    • 感谢您的回答。我试图按照你的方式修改它,但不幸的是,我在尝试从任务计划程序运行它时遇到了同样的错误(否则它再次工作得很好)。问题是,显然当从任务计划程序运行时,代码省略了“GetObject”行并在“CreateObject”行抛出错误。
    • 您需要检查TS的权限。显然他们还不够......
    • 你是完全正确的。我以“最高权限”运行任务,当我取消选中它时,脚本正常执行。
    【解决方案3】:

    请遵循以下:

    1) 在保存为 SendEmail.xlsm 的 excel 文件中写入您的 Sub:

    Option Explicit
    
    Public Sub send_email()
    
    
    
    Dim OutApp As Object
    Dim OutMail As Object
    
    Set OutApp = CreateObject("Outlook.Application") 
    Set OutMail = OutApp.CreateItem(0)
    
    On Error Resume Next
    With OutMail
        .to = "email@email.com"
        .CC = ""
        .BCC = ""
        .Subject = "subj"
        .Body = "body"
        .Attachments.Add ActiveWorkbook.FullName
        .Send
    End With
    On Error GoTo 0
    
    Set OutMail = Nothing
    Set OutApp = Nothing
    
    End Sub
    

    2)打开记事本写下这段代码并保存为vbs(SendEmail.vbs)

    Dim args, objExcel
    
    Set args = WScript.Arguments
    Set objExcel = CreateObject("Excel.Application")
    
    objExcel.Workbooks.Open args(0)
    objExcel.Visible = True
    
    objExcel.Run "send_email"
    
    objExcel.ActiveWorkbook.Save
    objExcel.ActiveWorkbook.Close(0)
    objExcel.Quit
    

    3)打开记事本写下这段代码并保存为bat(SendEmail.bat),我已经保存在我的桌面了,你可以随意保存。

    cscript "D:\desktop\SendEmail.vbs" "D:\desktop\SendEmail.xlsm"
    

    4) 在调度程序中创建一个调用 SendEmail.bat 的任务

    【讨论】:

    • 非常感谢!你的方法奏效了!我使用的是这里描述的方法:Excel:Running Excel on Windows Task Scheduler 但它不会像我上面提到的那样工作。我只剩下一个问题:在网站上描述的脚本中,如果有其他实例正在运行,则可以不退出 Excel,但是使用您的脚本,它仍然不会退出 Excel 的其他实例,我应该保持原样吗,还是使用那种检查方法?
    • 您要退出所有实例还是只退出脚本的实例?
    • 只有脚本的实例,这意味着我希望其他实例保持运行(以防有其他实例)。
    • CreateObject ("Wscript.Shell") 是干什么用的?
    • 尽管您的解决方案可能有效,但您未能确定 OP 问题,而且您也没有解释为什么您的答案是 OP 问题的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 2013-09-29
    • 2016-05-06
    相关资源
    最近更新 更多