Forum Discussion

vinodtc's avatar
vinodtc
Contributor
13 years ago

How to close Excel instances by using code

Hi,



I have written following code in OnStartTest Event. When there are 2 excel instances are there (Screen shot attached), it is unable to close those instances. Excel instance are showing in Object browser as Process("EXCEL",2)  and Process("EXCEL"). How do we close these instances?


Sub GeneralEvents_OnStartTest(Sender)

      

       while Sys.waitProcess("EXCEL", 10).Exists


                 Sys.Process("EXCEL").Close

      wend


 


     while Sys.waitProcess("iexplore",10).Exists


               Sys.Process("iexplore").Close


    wend 

End Sub



Regards,

Vinod



 

3 Replies


  • Hello Vinod,





    This problem can appear when the Excel process with index 1 is closed and does not exist, but TestComplete has not refreshed its internal objects tree yet. In this case, the code Sys.waitProcess("EXCEL", 10). Exists will return false while not all of Excel processes were closed. To avoid this situation, you can search for an Excel process irrespectively to its index using the Sys.FindChild method.







      Dim p, isClosed

      Set p = Sys.FindChild("ProcessName", "EXCEL")

      

      While p.Exists

        p.Close()

        'Wait until the process is closed.

        isClosed = p.WaitProperty("Exists", False)

        'If closing failed, terminate the process.

        If isClosed = False Then

          p.Terminate()

        End If

        Set p = Sys.FindChild("ProcessName", "EXCEL")

      Wend 

  • Hi,


    Thank you very much and above code is working as per my requirement.



    Regards,


    Vinod