使用任务计划程序运行VBS文件以启动Excel VBA宏

时间:2019-07-11 15:34:47

标签: excel vba vbscript taskscheduler

我有一个Excel VBA宏,该宏会更新Excel文件,基本上是遍历记录,以确定某些新员工在达到90天之前是否已在各个时间接受过培训,如果没有,则将电子邮件爆炸为通讯组提醒。我希望该宏每天自动运行一次,因此我的计算机将在每个记录的特定里程碑日期到来时浏览文件并启动电子邮件。

Excel宏运行完美。当我手动运行VBS文件时,它运行完美。

当我尝试从Task Scheduler运行(手动或计划)时,日志显示它已完成,但没有运行宏。

我尝试同时使用“仅在用户登录时运行”和“无论用户是否登录都运行”这两种方法,并且两者的结果相同。我的用户帐户(Bachenheimere)显示为用于运行任务的帐户,因此我显然有权运行该文件。

对于Task Scheduler,我正在使用“操作:启动程序”

程序脚本:"C:\Windows\SysWOW64\cscript.exe"

添加参数:"C:\Users\Bachenheimere\RunExcelMacroForCoachTracking.vbs"

Task Scheduler Pic1

Task Scheduler Pic2

同样,任务计划程序日志显示它在大约1.5分钟内成功完成(当我手动运行时,它在大约15-20秒内完成)。我不希望每天都不手动运行VBS文件,因此,如果有人可以帮助一个可怜的新手完成这项工作,我将不胜感激!

这是VBS文件代码:

Dim xlApp, xlBook


Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Users\Bachenheimere\file.txt",2,true)

Set xlApp = CreateObject("Excel.Application")
objFileToWrite.WriteLine(FormatDateTime(Now) & "Create Excel Object")
xlApp.Application.Visible = True
objFileToWrite.WriteLine(FormatDateTime(Now) & "App Visible = True")
xlApp.DisplayAlerts = False
objFileToWrite.WriteLine(FormatDateTime(Now) & "Display ALerts = False")
Set xlBook = xlApp.Workbooks.Open("H:\EmCare\Clinical Services\Provider Coaching\NEOU Tracking\Coach Trackingv3.xlsm", 0, True)
objFileToWrite.WriteLine(FormatDateTime(Now) & "Excel book opened")

xlApp.Run "Sheet1.SendReminderMail"
objFileToWrite.WriteLine(FormatDateTime(Now) & "Macro is run")

xlBook.Save
objFileToWrite.WriteLine(FormatDateTime(Now) & "Macro is saved")


xlBook.Close
objFileToWrite.WriteLine(FormatDateTime(Now) & "Book closed") 

xlApp.Quit
objFileToWrite.WriteLine(FormatDateTime(Now) & "App Quit")

objFileToWrite.Close
Set objFileToWrite = Nothing

set xlBook = Nothing
Set xlApp = Nothing

WScript.Echo "Your Automated Task successfully ran at " & TimeValue(Now)
WScript.Quit

0 个答案:

没有答案