问题
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