2017-08-28 117 views
0

我創建了一個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 感謝您的幫助!

阿克沙伊

回答

0

一些谷歌搜索帶來了這樣的結果: https://www.devhut.net/2014/10/31/createobjectoutlook-application-does-not-work-now-what/

筆者並沒有說明爲什麼它是因爲它與其它微軟Office軟件做自動化的前景將不作爲無縫工作的情況下,但他提供了另一種綁定Outlook實例的方法,也許這將解決您的問題。

(只有一個鏈接,沒有代碼,因爲作者明確表示不要重新發布他的代碼)

+0

棒極了!那就是訣竅。非常感謝你! – user3304587

相關問題