[英]Excel VBA code throws run-time error 429 while sending Outlook email using Task Scheduler
[英]Create email from PowerShell using Task Scheduler (Runtime Error 429)
在执行某些分析后,我创建了几个脚本来发送电子邮件。 这在以前有效。 现在一些脚本失败并给出以下错误消息:
运行时错误“429”:
ActiveX 组件无法创建对象
然后它将 Outlook 对象代码行称为错误。
该脚本在我退出 MS Outlook 时运行,如果 Outlook 正在运行,则该脚本会出错。 我使用任务计划程序来运行它,Outlook 通常会运行。
Private Sub Send_Ratings_Email()
Dim OutApp As Object, OutMail As Object
Dim rng As Range
Dim StrBody As String
StrBody = "Please find Maturities for the Current Week Below: "
Application.ScreenUpdating = False
On Error Resume Next
Set OutApp = GetObject(, "Outlook.Application")
On Error GoTo 0
If OutApp Is Nothing Then Set OutApp = GetObject("Outlook.Application")
On Error GoTo cleanup
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
Call AddAzureLabel(OutMail, "Restricted - Internal")
With OutMail
.To = " example.com"
.Subject = "Weekly Maturities - W/C " & Format(Now, "dd-mmm-yy")
.HTMLBody = StrBody & RangetoHTML(rng)
.Display 'Or use .Send
End With
On Error GoTo 0
Set OutMail = Nothing
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
Powershell 脚本在手动运行时有效。
您正在创建 Outlook 实例,如果它无法运行,请检查它是否已经在运行并获取正在运行的应用程序,如果没有,则创建一个:
On Error Resume Next
Set OutApp = GetObject(, "Outlook.Application")
On Error GoTo 0
If OutApp Is Nothing Then Set OutApp = CreateObject("Outlook.Application")
在您的任务计划程序任务中取消勾选“以最高权限运行”
暂时将其添加为答案,但实际上只是尝试以不适合评论的方式进行调试。 与其他答案类似,但想法是使用它来创建一个测试用例,让我们看看我们是否可以让它在您的 Task Scheduler>Powershell 用例中工作。
让您的 PS 在此处运行foo
或foo2
过程,并且我已将 Outlook 实例化封装为独立函数,以尝试保持干净并隔离错误。
Option Explicit
Sub foo()
Dim olApp As Object
Set olApp = GetApplication("Outlook.Application")
End Sub
Sub foo2()
Dim olApp As Object
Set olApp = GetApplication2("Outlook.Application")
End Sub
Function GetApplication2(className As String) As Object
' function to encapsulate the instantiation of an application object
Dim theApp As Object
On Error Resume Next
Set theApp = CreateObject(className)
If theApp Is Nothing Then
MsgBox "Unable to get a handle on " & className
Else
MsgBox "Successfully got a handle on " & className & ", returning to caller"
End If
Set GetApplication2 = theApp
End Function
Function GetApplication(className As String) As Object
' function to encapsulate the instantiation of an application object
Dim theApp As Object
On Error Resume Next
Set theApp = GetObject(, className)
If Err.Number <> 0 Then
MsgBox "Unable to Get" & className & ", attempting to CreateObject"
Set theApp = CreateObject(className)
End If
If theApp Is Nothing Then
Err.Raise Err.Number, Err.Source, "Unable to Get or Create the " & className & "!"
Set GetApplication = Nothing
End If
MsgBox "Successfully got a handle on Outlook Application, returning to caller"
Set GetApplication = theApp
End Function
我正在从 PS 运行,例如:
$excel = new-object -comobject excel.application
$file = "C:\debug\ps-excel.xlsm"
$wb = $excel.workbooks.open($file)
$excel.Run("Module1.foo")
和:
$excel.Run("Module1.foo2")
在foo
过程中, GetObject
引发错误,但它不是 429 错误,并且控制传递给成功实例化应用程序的CreateObject
。 因此,不幸的是,我无法在foo
/ foo2
任何组合下重现错误,并且 Outlooks 是否已经打开似乎也无关紧要。
所以,这有点 hack-y 因为我认为它不能解决根本原因(并且因为我无法复制您的错误,它实际上可能无法解决任何问题)但值得一试,正如您所指出的:
重要的是要注意,脚本会在我退出 MS Outlook 时运行,如果 Outlook 已经在运行,则会出现错误
我的想法是修改你的 PS 来检查正在运行的 Outlook 实例并通过stop-process
Cmdlet 关闭它。
try {
stop-process -Name "outlook" -ErrorAction Stop
Write-Host "Stopped the running instance of Outlook Application"
} catch {
Write-Host "outlook was not already running"
}
$excel = new-object -comobject excel.application
$file = "C:\debug\ps-excel.xlsm"
$wb = $excel.workbooks.open($file)
$excel.run("Module1.foo")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.