Excel Window Handle

Add-in Express™ Support Service
That's what is more important than anything else

Excel Window Handle
 
Micha? G??bowski


Guest


Dear Sirs,
I'd like to get Excel main window Handle to be able to use it to set AlwaysOnTop property with code like:
SetWindowPos(WHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE)

Problem is that ExcelApp.ActiveWindow and no other has HWND Handle exposed. How to achieve this?
Micha?.
Posted 10 Mar, 2016 04:54:49 Top
Andrei Smolin


Add-in Express team


Posts: 18826
Joined: 2006-05-11
Micha?,

What Excel version does your add-in support?


Andrei Smolin
Add-in Express Team Leader
Posted 10 Mar, 2016 06:15:36 Top
Micha? G??bowski


Guest


I am going to support Excel 2007+ (Now using 2007). All with ribbons. M.
Posted 10 Mar, 2016 07:10:49 Top
Andrei Smolin


Add-in Express team


Posts: 18826
Joined: 2006-05-11
Micha?,

What do you need to achieve? At what moment are you going to call SetWindowPos? In the Click event of a button?


Andrei Smolin
Add-in Express Team Leader
Posted 10 Mar, 2016 07:20:16 Top
Micha? G??bowski


Guest


Andrei, here you have something in VBA with similar functionality:
http://www.extendoffice.com/documents/excel/2030-keep-excel-window-on-top.html

Here is a sketch of my sub-function to use i.e. in "Always On Top" TCheckBox.OnChange:


  procedure SetTopMost(WHandle: HWND; Value: Boolean);
  begin
    if Value then
      SetWindowPos(WHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE)
    else
      SetWindowPos(WHandle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE);
  end;

  ...

  SetTopMost(MyExcel.Handle, MyCheckBox.Checked);
Posted 10 Mar, 2016 07:26:27 Top
Andrei Smolin


Add-in Express team


Posts: 18826
Joined: 2006-05-11
Thank you. You can use the GetActiveWindow function from the Windows API.

Note that Excel 2013-2016 implements the SDI interface (one Excel.Window object per window). Accordingly, in these Excel versions they've provided Window.Hwnd property, see https://msdn.microsoft.com/en-us/library/office/dn292510%28v=office.15%29.aspx.


Andrei Smolin
Add-in Express Team Leader
Posted 10 Mar, 2016 09:21:59 Top
Micha? G??bowski


Guest


Thank you Andrei,
I used your hint with GetActiveWindow and it worked perfectly. I thought that if you communicated with Excel at low level it would have been easy to get Handle of its window. However thank you.
Michal
Posted 12 Mar, 2016 16:09:34 Top
Andrei Smolin


Add-in Express team


Posts: 18826
Joined: 2006-05-11
Hello Micha?,

Micha? G??bowski writes:
I thought that if you communicated with Excel at low level it would have been easy to get Handle of its window.


We will consider providing a property/method. Thank you!


Andrei Smolin
Add-in Express Team Leader
Posted 14 Mar, 2016 01:02:54 Top