Forum Discussion

mesg2anil's avatar
mesg2anil
Regular Contributor
14 years ago

Close Excel Sheet When TC Script Breaks

Hi,



I want to know if there is a way to close the excel sheet whenever script breaks / stops  due to either error in the script or application not responding or of for any reason...?



Whenever there is an error, TC stops and write the log and displays it... before it does that, I want to perform some action like closing the excel sheet... is that possible???
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Use the "OnLogError" event handler and, in that handler, detect of the Excel sheet is open.  If it is, close it.
  • mesg2anil's avatar
    mesg2anil
    Regular Contributor
    Hi Robert,



    I added and event for "OnLogError" as you said, below is the script for the same... but it is erroring out saying "Invalid number of "OnLogError" event handler parameters." Please suggest!!




    Sub GeneralEvents_OnLogError'(Sender, LogParams)

    'Close all browser or excel file or any process which you want to terminte all together in one go



    GeneralEvents_OnLogError

    IExplore = Sys.FindAllChildren("ProcessName","EXCEL")


        If UBound(IExplore)>=0 Then


        For i = UBound(IExplore) to LBound(IExplore) Step -1 ' <== iterate from top to bottom


            Set Excel = Sys.OleObject("Excel.Application")


            Call Excel.ActiveWorkbook.Save


            Call Excel.Workbooks.Close


            IExplore(i).Terminate


        Next


        End If



    End Sub


  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    I don't know if you intended it, but take a look at your code:




    Sub GeneralEvents_OnLogError'(Sender, LogParams)







    Notice that all the parameters on the declaration statement have been commented out.  Remove the apostrophe and that should correct it.



    Those parameters are required for OnLogError event handlers to work.  You may not use them necessarily, but they are part of the event handler.

  • mesg2anil's avatar
    mesg2anil
    Regular Contributor
    Hi Robert...



    Its working now...

    Thanks a lot for the needful...