VBS不停止运行 [英] VBS Doesn't stop running

查看:448
本文介绍了VBS不停止运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计划的任务,启动一个vb脚本,反过来在excel中运行一个宏。一切运行良好(因为我得到我想要的结果),但是,计划的任务仍然运行,因此在第二天早上不会再次启动。 VB脚本如下所示:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆