How to get an object reference to an existing instance of Excel?
Hello, our AUT allows users to open a spreadsheet by clicking on a button/link.
I'm trying to automate the validation of this function. So after the button is selected, I want to get an object reference to the Excel instance (that should be created) and validate that the correct ActiveWorkbook and ActiveSheet have, in fact, been loaded.
The problem I'm having is that when I establish the object reference to Excel, it creates another Excel instance (that I can see in Task Manager), instead of referencing the already existing instance. The script validation fails at this point since the expected workbook and sheet are not loaded into the new (second) instance.
I'm running on a Win7 64 bit OS with Excel 2016 (32 bit). TestComplete is 12.0.122.7.
Here's the VBscript code I'm trying to use...
Function WorkSheetActive2(sWorkBookName, sWorkSheetName, bCloseWorkBook)
bSheetActive = True
sMsg = "Expected Workbook and Worksheet [" &sWorkBookName& " : " &sWorkSheetName& "] are active."
'None of the following capture the existing Excel instance...
Set Excel = Sys.OleObject("Excel.Application")
'Set Excel = GetObject("","Excel.Application")
'Set Excel = GetObject("Excel.Application")
'Set Excel = GetObject("C:\Temp\TestFile1.xls","Excel.Application")
If (Excel.Workbooks.Count > 0) Then
If (Ucase(Excel.ActiveWorkbook.Name) = Ucase(sWorkBookName)) then
If (Ucase(Excel.ActiveSheet.Name) <> Ucase(sWorkSheetName)) then
bSheetActive = False
End If
Else
bSheetActive = False
End If
Else
bSheetActive = False
End If
Call LogAction (bSheetActive, sMsg)
If (bSheetActive = True) and (bCloseWorkBook = True) Then
Call LogAction (bSheetActive, "Closing active workbook.")
Excel.ActiveWorkbook.Close
End If
WorkSheetActive = bSheetActive
End Function
Hi,
If I got your question right... Some time ago I had exactly the same question and the great link provided by Support helped: https://smartbear-cc.force.com/portal/KbArticleViewer?name=Get-COM-reference-for-a-running-Excel-instance&sp=testcomplete