Forum Discussion

Anastasia02's avatar
Anastasia02
New Contributor
25 days ago

Sys.OleObject. Microsoft Office (Word, Excel)

I want to get the properties and methods of an already running Microsoft Office (Word, Excel) application.

For this I use OLE object but I get an error "This command is not available because no document is open."

I take into account what is written in the documentation:

Note for Microsoft Office users: In order to be able to get an OLE object for an already running application, this application must be registered in the Running Object Table (ROT). However, Microsoft Office applications that are launched from the shell (for example, from the Start menu or the TestedApps project item) do not register their running objects at startup, but rather once the application loses focus. So, if you obtain an OLE object for an already running Office application, you may get errors when accessing its properties and methods (for more information on this problem, see http://support.microsoft.com/kb/238610/).

To work around the problem, you can activate any window other than the tested Office application before attaching to its OLE server. This will allow the Office application to register itself in the ROT, so you will be able to work with it via OLE. In scripts, you can activate a window using the Activate or Click action. For example, use the following statement to activate the Windows taskbar

This worked in TestComplete9 but doesn't work now (TestComplete15). Maybe there is some solution for this case? I use DelphiScript

2 Replies

  • rraghvani's avatar
    rraghvani
    Icon for Champion Level 3 rankChampion Level 3

    I'm not able to get this to work either, if I already have Excel opened. I would expect the spreadsheet to open.

    function test4()
    {
        let Excel = Sys.OleObject("Excel.Application");
        //let Excel = getActiveXObject("Excel.Application");
        Sys.Process("explorer").Window("Shell_TrayWnd", "", 1).Activate();
        //Excel.Visible = true;
        Excel.Workbooks.Open("C:\\Temp\\Book2.xlsx");
        aqUtils.Delay(5000);
        Excel.Quit();
    }

    I suggest you open a support ticket via https://support.smartbear.com/testcomplete/message/

     

  • Hello,

    The key is to use the Sys.WaitProcess method to wait for the application to appear, then use a script to open a new document or confirm one is open before trying to get the OLE object.

    Launch the Application and Wait: Start the Office application (Word or Excel) from your TestComplete script as you normally would. Use Sys.WaitProcess to ensure the process is running and accessible.

    Ensure a Document is Open: This is the most crucial step. Use your script to activate the application and either open a new document or verify that an existing document is open. This action forces the application to register itself in the ROT.

    Get the OLE Object: Once you've confirmed a document is open, you can proceed to get the OLE object and access its properties and methods.

    procedure GetOfficeOLEObject;
    var
      wordApp: OleVariant;
      wordProcess: OleVariant;
      docCount: Integer;
    begin
      // Step 1: Launch Word and wait for the process to be available
      wordProcess := Sys.WaitProcess('WINWORD'); 
      
      // Activate the Word application window to ensure it registers in the ROT
      wordProcess.Window('#0', '', 1).Activate;

      // Wait a moment for the activation to take effect
      aqUtils.Delay(2000); 

      // Step 2: Ensure a document is open. If no documents are open, create a new one.
      wordApp := Sys.OleObject('Word.Application');

      // Check the number of open documents. The Documents property is the key.
      docCount := wordApp.Documents.Count;
      
      // If no document is open, create a new one to enable OLE functionality.
      if docCount = 0 then
      begin
        Log.Message('No Word document is open. Creating a new one.');
        wordApp.Documents.Add;
        // It's good practice to wait for the new document to be fully ready
        aqUtils.Delay(1000); 
      end;

      // Step 3: Now you can safely access properties and methods
      Log.Message('Word version is: ' + wordApp.Version);
      Log.Message('Active document name is: ' + wordApp.ActiveDocument.Name);

      // Example of a task: writing text to the active document
      wordApp.Selection.TypeText('Hello from TestComplete!');
      
      // Clean up by closing the application (or save/close the document first if needed)
      wordApp.Quit;
      Log.Message('Word application has been closed.');
    end;

    The old "activate another window" workaround often failed because it didn't guarantee that a document was open within the Office application. The core issue, as highlighted in the Microsoft support article you cited, is that the Office application's OLE automation server (the part that provides properties and methods) is often only fully functional when a document is present. Without an open document, many commands like Application.ActiveDocument will fail.