Windows Task Manager Schedule VBA Macro to Send An Email Using OutLook Daily Running Manually and Not Automatically

拈花ヽ惹草 提交于 2020-03-03 10:11:09

问题


I have my macro which is running, & for this I created a vb script and used windows task manager to schedule its run every day.

Whenever I run manually or attempt changing the time in the trigger, I always make sure that both excel and outlook are not running. If I run the macro in Excel VBA, it sends the email. After scheduling the task to run everyday, just as a test if I go to (in TaskScheduler) View -> Hidden Tasks and manually click Run, it sends the email.

However, if I schedule it to run at a specific time everyday, say maybe starting today, 15 minutes from now, it does not send the email & the last run result is (0x0).

I have enabled all macros in the trust center settings and selected 'Trust access to the VBA project object model', & it's not an issue about administrator privileges. This is my VB script

Dim ObjExcel, ObjWB
Set ObjExcel = CreateObject("Excel.Application")
Set ObjWB = ObjExcel.Workbooks.Open("C:\Users\myUser\MyLocation\MyFile.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 "MyFile.xlsm!main_macro"
ObjWB.Save
ObjWB.ActiveWorkbook.Close
ObjExcel.Quit

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

Also, here are my TaskScheduler Settings:

Ref:

Sending email from excel automatically based on date

How to set recurring schedule for xlsm file using Windows Task Scheduler

How can you run an Excel macro through a schedule task

Task Scheduler does not run Excel VBA Code to send PDF as Email Attachment

If I set the below to True:

ObjExcel.Visible = False
ObjExcel.DisplayAlerts = False
ObjExcel.AskToUpdateLinks = False
ObjExcel.AlertBeforeOverwriting = False

Excel opens in read-only mode and says click notify to receive the notification that you can now edit the workbook but I cannot edit it, says another user has the workbook open for editing.


回答1:


I think the point is that the line

ObjWB.ActiveWorkbook.Close

must be

ObjWB.Close

.

Please try this and comment your results.



来源:https://stackoverflow.com/questions/40610636/windows-task-manager-schedule-vba-macro-to-send-an-email-using-outlook-daily-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!