我正在尝试运行下面的代码。当我在Windows中双击.vbs脚本时,它确实完成了它需要做的事情。保存并关闭所有打开的excel实例,然后打开2张指定的工作表,在工作表中运行宏并关闭它们。当计算机进入Wi-Fi范围时,我使用excel中的Auto_Open来同步这些工作表。当我尝试通过.bat或直接使用Windows任务调度程序运行命令时。它需要保存和关闭所有打开的Excel工作表的第一部分似乎不起作用。其他一切都像魅力一样。我缺少任务调度程序的基本功能吗?代码如下:
On Error Resume Next
While Err.Number = 0
Set objOffice = GetObject(,"Excel.Application")
set objShell = CreateObject("Wscript.Shell")
objOffice.DisplayAlerts = False
For Each objWindow In objOffice.Windows
objWindow.Activate
Set WBook = objOffice.ActiveWorkbook
WBook.Saved = True
WBook.Save
WBook.Close
Next
objOffice.DisplayAlerts = True
objOffice.Quit
Set objOffice = Nothing
WScript.Sleep 2000
Wend
set objShell = CreateObject("Wscript.Shell")
Set objOffice = CreateObject("Excel.Application")
WScript.Sleep 2000
objOffice.visible=True
set WBook = objOffice.Workbooks.Open("C:\Users\Public\Documents\OffSide Log Storage\Ron Charawich - Offside Transmitter Log.xls",0)
objOffice1.Run "Ron Charawich - Offside Transmitter Log.xls!Auto_Open"
Set objOffice = Nothing
WScript.Sleep 30000
On Error Resume Next
While Err.Number = 0
Set objOffice = GetObject(,"Excel.Application")
set objShell = CreateObject("Wscript.Shell")
objOffice.DisplayAlerts = False
For Each objWindow In objOffice.Windows
objWindow.Activate
Set WBook = objOffice.ActiveWorkbook
WBook.Saved = True
WBook.Save
WBook.Close
Next
objOffice.DisplayAlerts = True
objOffice.Quit
Set objOffice = Nothing
WScript.Sleep 2000
Wend
set objShell = CreateObject("Wscript.Shell")
Set objOffice = CreateObject("Excel.Application")
WScript.Sleep 2000
objOffice.visible=True
set WBook = objOffice.Workbooks.Open("C:\Users\Public\Documents\Tablet Sheet Storage\Nabiye_PassFail_2016_Field_Locked_Tablet.xlsm",0)
objOffice.Run "Nabiye_PassFail_2016_Field_Locked_Tablet.xlsm!Auto_Open"
Set objOffice = Nothing
WScript.Quit