VBA运行时错误:249 - 仅在Schtasks时发生

时间:2014-01-17 01:31:18

标签: excel vba excel-vba

编辑:仅当Outlook实例已在运行时才会发生此错误。

这是一个奇怪的错误。我已经设置了一个宏,可以在下载报告后每天早上自动执行。

宏由此VBS代码执行:

Dim xl
Set xl = CreateObject("Excel.Application")

Dim WshShell, strCurDir
Set WshShell = CreateObject("WScript.Shell")
strCurDir    = WshShell.CurrentDirectory

xl.Application.Workbooks.Open(strCurDir & "\TodaysScript.xlsm")
xl.Application.Visible = False
xl.Application.run "'TodaysScript.xlsm'!autoOpen"

xl.DisplayAlerts = False
xl.Quit

Set xl = Nothing
Set WshShell = Nothing

autoOpen是我正在运行的宏的名称(我故意避免Auto_Open

当我运行脚本时,一切都很有趣,但是当脚本通过schtasks执行时,它将返回此错误:

enter image description here

调试时,它会显示在此处:

enter image description here

我做了很多阅读,知识库文章没有帮助。

如果我双击我的VBS并正常执行它会如何工作,这很奇怪,但是当我通过schtasks执行它时,它会出现错误。

这是schtask查询

schtasks /create /tn "Reports" /tr %file% /sc weekly /st 10:00:00 /d MON,TUE,WED,THU,FRI /V1

我可能还会补充道:这在昨天有效,但现在它已停止在我的电脑和大学的计算机上工作了。

1 个答案:

答案 0 :(得分:0)

正如您对问题的评论中所述,如果是权限问题,您可以更改运行该命令时运行的用户,这是语法

schtasks /Create 
[/S system [/U username [/P [password]]]]
[/RU username [/RP [password]] /SC schedule [/MO modifier] [/D day]
[/M months] [/I idletime] /TN taskname /TR taskrun [/ST starttime]
[/RI interval] [ {/ET endtime | /DU duration} [/K] 
[/XML xmlfile] [/V1]] [/SD startdate] [/ED enddate] [/IT] [/Z] [/F]

来源:http://msdn.microsoft.com/en-us/library/windows/desktop/bb736357(v=vs.85).aspx