I need to obtain the Excel 2013 x64 window handle from 64 bit VBA code running in a spreadsheet. There are a couple of options to do this:
- Read
Application.Hwnd
(MSDN Application.Hwnd Property (Excel)) - Call FindWindow, imported from user32, e.g. as described in the accepted answer here: What are the differences between VBA 6.0 and VBA 7.0?:
Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _ ByVal lpClassName As String, _ ByVal lpWindowName As String) As LongPtr
The problem is that Application.Hwnd
returns a Long
, i.e. 32 bits (I've verified this with MsgBox TypeName(Application.Hwnd)
within a 64 bit environment), whereas FindWindow
returns a LongPtr
, which is 64 bits long in Office x64.
Does this mean that the Application.Hwnd
property can't be trusted to always be correct in a 64 bit environment?