VBS不停止运行 [英] VBS Doesn't stop running
问题描述
Option Explicit
Dim xlApp,xlBook
设置xlApp = CreateObject(Excel.Application)
设置xlBook = xlApp.Workbooks.Open(C:\MyPath\My File.xlsm,0,True)
xlApp .RunmyMacro
xlBook.Close
xlApp.Quit
设置xlBook =没有
设置xlApp =没有
WScript.Quit
宏的结尾(所有上述代码工作并根据需要保存我的csv输出):
工作簿(My File.xlsm)。激活
工作表(输出)。选择
'ActiveWorkbook.Save
'ActiveWorkbook.Close - 此行导致vbs在创建我的csv
后失败Application.ScreenUpdating = True
Application.DisplayAlerts = True
结束Sub
如何终止VB脚本来停止计划的任务运行?
不幸的是,您意识到:
工作簿(My File.xlsm)激活
工作表(输出)。选择
'ActiveWorkbook.Save
'ActiveWorkbook.Close - 此行导致vbs在创建我的csv
后失败Application.ScreenUpdating = True
Application.DisplayAlerts = True
由于xlBook对象试图尝试关闭已经关闭的工作簿。我相信如果你更新VBScript到:
Option Explicit
Dim xlApp,xlBook
设置xlApp = CreateObject(Excel.Application)
设置xlBook = xlApp.Workbooks.Open(C:\MyPath\My File.xlsm,0,True)
xlApp.RunmyMacro
'xlBook.Close删除此行
xlApp.Quit
设置xlBook =没有
设置xlApp =没有
WScript.Quit
并添加ActiveWorkbook.Save和ActiveWorkbook.Close行,VBScript &安培; Excel应用程序应正确关闭。
I have a scheduled task that launches a vb script which in turn runs a macro within excel. Everything runs fine (as in I get the results I want), however, the scheduled task remains 'running' and therefore doesn't start again the next morning. The VB Script is as follows:
Option Explicit
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\MyPath\My File.xlsm", 0, True)
xlApp.Run "myMacro"
xlBook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
WScript.Quit
The end of the Macro (all the preceding code work and saves my csv output as required):
Workbooks("My File.xlsm").Activate
Worksheets("Outputs").Select
'ActiveWorkbook.Save
'ActiveWorkbook.Close - this line cause vbs to fail after creating my csv
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
How do I terminate the VB Script to stop the scheduled task from running?
Unfortunately as you realized:
Workbooks("My File.xlsm").Activate
Worksheets("Outputs").Select
'ActiveWorkbook.Save
'ActiveWorkbook.Close - this line cause vbs to fail after creating my csv
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Will give you an error due to the fact that the xlBook object is attempting to close the already closed workbook. I believe that if you update VBScript to:
Option Explicit
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\MyPath\My File.xlsm", 0, True)
xlApp.Run "myMacro"
'xlBook.Close Remove this line
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
WScript.Quit
and add back the ActiveWorkbook.Save and ActiveWorkbook.Close lines, you VBScript & Excel application should close correctly.
这篇关于VBS不停止运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!