Skip to main content

Content Starts Here

Get COM reference for a running Excel instance

When Excel is started by some other application, you may want to take a COM reference for that running instance instead of creating a new instance. But, using functions like Sys.OleObject, CreateObject, or GetObject runs a new instance of Excel.
The simple answer is No - you cannot get a COM reference to a running Excel instance using GetActiveObject, GetObject or similar functions. Excel is a single-instance COM server - whenever you ask for a reference, you get a new instance. That's how single-instance COM servers work.

Luckily, Excel itself has a hidden feature - it can return a COM reference to itself through one of its windows when you ask that window for a Microsoft Active Accessibility reference (IAccessible interface).

Here is what you can do in order to get that COM reference by using the hidden feature:

1. Add the "EXCEL7" window class name to your TestComplete project MSAA settings and enable it:

Project Properties | MSAA

2. Now, you can see MSAA properties for that window in the Object Browser and, specifically, the NativeObject property:
MSAA Properties, NativeObject

This is the property that will give us a COM reference at the end.

3. Now, in your script, you can get a COM reference to this Excel instance. In the script examples above, objExcel is the Excel application instance COM reference.


function ExcelTest()
  var p = Sys.Process("EXCEL");
  var coolWindow = p.Window("XLMAIN").Window("XLDESK").Window("EXCEL7");
  var objExcel = coolWindow.NativeObject.Application;

  // Now we can close Excel having that reference



Sub ExcelTest
  Dim p, coolWindow, objExcel

  Set p = Sys.Process("EXCEL")
  Set coolWindow = p.Window("XLMAIN").Window("XLDESK").Window("EXCEL7")
  Set objExcel = coolWindow.NativeObject.Application

  ' Now we can close Excel having that reference
End Sub


def ExcelTest():
  p = Sys.Process("EXCEL")
  coolWindow = p.Window("XLMAIN").Window("XLDESK").Window("EXCEL7")
  objExcel = coolWindow.NativeObject.Application

  # Now we can close Excel having that reference
Previous MonthNext Month