VBS Doesn't stop running

后端 未结 1 1983
小蘑菇
小蘑菇 2021-01-25 14:51

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 re

相关标签:
1条回答
  • 2021-01-25 15:28

    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.

    0 讨论(0)
提交回复
热议问题