Post your working scripts, libraries and tools
Posts: 1760
Joined: 29 Sep 2013, 17:11


16 May 2017, 17:17

Returns an Excel application object.

App := Excel_Get([WinTitle, Excel7#])

WinTitle - Optional. Specify a WinTitle to match. Or omit the WinTitle to use the default "ahk_class XLMAIN" which will get the active window.
Excel7# - Optional. Default is 1. Don't change this unless you know what it does.

[ github ] [ download ] (Right-click > SaveAs)

Example Usage:

Code: [Select all] [Download] GeSHi © Codebox Plus

F7::  ; Press F7 to display Excel's caption.
xlApp := Excel_Get()
if !IsObject(xlApp) ; If Excel_Get fails it returns an error message instead of an object.
MsgBox, 16, Excel_Get Error, % xlApp
MsgBox, % "Caption: " xlApp.Caption

Why Excel_Get instead of ComObjActive:
  • It can match a WinTitle.
  • If Excel is in edit-mode, attempting a COM action will produce an error. Excel_Get detects this error and uses ControlSend to send {Escape} to the workbook window.
  • ComObjActive returns the "active" object from the running object table (ROT). "Active" on the ROT does not mean that the window is active. So if more than one Excel application/process is running, ComObjActive does not necessarily return the application object of the active Excel window.
  • If Excel was just opened it may not have registered itself on the ROT yet.

Thanks to jethrow; this version is based on his version. I have rearranged some things but the logic remains largely unchanged. However, unlike other versions, this function is standalone. The ACC lib IS NOT REQUIRED. Also unlike other versions, this function returns a specific error message in the event of failure.
Last edited by kon on 15 Jul 2017, 13:42, edited 4 times in total.
Posts: 2121
Joined: 09 Oct 2013, 10:31

Re: Excel_Get

16 May 2017, 23:34

nice work

Posts: 7
Joined: 02 May 2016, 20:38

Re: Excel_Get

17 May 2017, 02:29

great! This helps.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 13 guests