VBS脚本可以手动运行,但不能通过调度程序运行

VBS脚本可以手动运行,但不能通过调度程序运行

问题描述:

我创建了一个VBA脚本,用于在引发时发送图形的电子邮件。我正试图设置一个计划的工作,每天早上9:30发送电子邮件。VBS脚本可以手动运行,但不能通过调度程序运行

我创建了一个运行良好的VBS脚本,当我调用它(即cscript.exe EmailDailyBurnDown.VBS)时,但通过调度程序引发时该脚本不起作用。你能帮我吗?

EmailDailyBurnDown.VBS

Dim ObjExcel, ObjWB 
Set ObjExcel = CreateObject("Excel.Application") 
Set ObjWB = ObjExcel.Workbooks.Open("C:\20170814_Promotion Work Backlog_V1.0.9.xlsm") 
ObjExcel.Visible = False 
ObjExcel.DisplayAlerts = False 
ObjExcel.AskToUpdateLinks = False 
ObjExcel.AlertBeforeOverwriting = False 

'vbs opens a file specified by the path below 
'either use the Workbook Open event (if macros are enabled), or Application.Run 

ObjExcel.Application.Run "SendBurnDownChartViaEmail" 
ObjWB.Save 
ObjWB.Close 
ObjExcel.Quit 

Set ObjWB = Nothing 
Set ObjExcel = Nothing 
WScript.Echo "Finished." 
WScript.Quit 

VBA脚本

Sub SendBurnDownChartViaEmail() 

    Dim OutApp As Object 
    Dim OutMail As Object 
    Dim Fname As String 

    Set OutApp = CreateObject("Outlook.Application") 
    Set OutMail = OutApp.CreateItem(0) 

    'File path/name of the gif file 
    Fname = Environ$("temp") & "\My_Sales1.gif" 

    'Save Chart named "Chart 1" as gif file 
    ActiveWorkbook.Worksheets("Hidden").ChartObjects("Chart 1").Chart.Export _ 
      Filename:=Fname, FilterName:="GIF" 
    'MsgBox (Fname) 

    On Error Resume Next 
    With OutMail 
     .To = "[email protected]" 
     .CC = "" 
     .BCC = "" 
     .Subject = "FBT Sprint " & Worksheets("5. Capacity & Sprint Planning").Range("E11").Value & " Burn Down - " & Date 
     .Attachments.Add Fname 
     .HTMLBody = "<html>" & "<img src='cid:My_Sales1.gif'></html>" 
     .Send 'or use .Display 
     '.Display 
    End With 
    On Error GoTo 0 

    'Delete the gif file 
    Kill Fname 

    Set OutMail = Nothing 
    Set OutApp = Nothing 

End Sub 

我碰到下面的错误。 enter image description here enter image description here 感谢您的帮助!

阿克沙伊

一些谷歌搜索带来了这样的结果: https://www.devhut.net/2014/10/31/createobjectoutlook-application-does-not-work-now-what/

笔者并没有说明为什么它是因为它与其它微软Office软件做自动化的前景将不作为无缝工作的情况下,但他提供了另一种绑定Outlook实例的方法,也许这将解决您的问题。

(只有一个链接,没有代码,因为作者明确表示不要重新发布他的代码)

+0

棒极了!那就是诀窍。非常感谢你! – user3304587