binding a Windows form to an Office app

  • Thread starter Patrick Schmid [MVP]
  • Start date
P

Patrick Schmid [MVP]

I have a .net Office add-in, and want to bind a Windows Form of it to
the
Office app window. How do I do that?

Thanks,

Patrick Schmid
 
X

XL-Dennis

Hi Patrick,

I've not yet used the following for a .NET-based solution but it works well
in classic COM Add-ins and with Excel.

'Declare the following API in a module:
Public Declare Function FindWindow Lib "user32.dll" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Public Declare Function SetWindowLong Lib "user32.dll" _
Alias "SetWindowLongA" ( _
ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long

'Declare the following variables as well:
Public Const CONST_HWIND As Long = -8
Public CONST_XLWIND As Long

In the OnConnection event add the following:
'Here we grab the Main window for Excel.
CONST_XLWIND = FindWindow(vbNullString, xlApp.Caption)

'In a function or Sub that load and show the form add the following:
Load frmE
SetWindowLong frmE.hwnd, CONST_HWIND, CONST_XLWIND
frmE.Show vbModeless

'frmE refer to the actual form we target.

Please test the above and let me know if it works with .NET as well.

---------------
With kind regards,
Dennis
Weekly Blog .NET & Excel: http://xldennis.wordpress.com/
My English site: http://www.excelkb.com/default.aspx
My Swedish site: http://www.xldennis.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top