gpt4 book ai didi

使用任务计划程序发送 Outlook 电子邮件时 Excel VBA 代码引发运行时错误 429

转载 作者:行者123 更新时间:2023-12-03 03:29:39 28 4
gpt4 key购买 nike

我正在使用 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

最佳答案

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

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

关于使用任务计划程序发送 Outlook 电子邮件时 Excel VBA 代码引发运行时错误 429,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46443745/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com