Two-way MS Word Automation

I am embedding MS Word in my application using the Win32 SetParent function. Everything works fine, but there is a requirement to make a callback from the Word custom toolbar button for the parent application. The Word instance is built into the user control, therefore parent this.Handle.

The VBA code is as follows:

Sub Submit() Dim hwnd As Long hwnd = FindWindow("Opusapp", vbNullString) hwnd = GetAncestor(hwnd, GA_PARENT) If hwnd = 0 Then MsgBox "Failed to callback!" Exit Sub End If OutputDebugString ("Parent window " + CStr(hwnd)) Dim id As Long id = RegisterWindowMessage("__CALLBACK_FROM_WORD__") If hwnd = 0 Then MsgBox "Failed to callback. Message not registered" Exit Sub End If OutputDebugString ("Message " + CStr(id)) End Sub 

In C #, the code is this:

  protected override void OnHandleCreated(EventArgs e) { submitMessageId_ = RegisterWindowMessage("__CALLBACK_FROM_WORD__"); base.OnHandleCreated(e); } protected override void OnHandleDestroyed(EventArgs e) { base.OnHandleDestroyed(e); } protected override void WndProc(ref Message m) { if (m.Msg == submitMessageId_) { Logger.Instance().Write("WndProc: Submit event"); return; } base.WndProc(ref m); } 

The problem is that VBA cannot find the correct window handle. I tried using GetParent to no avail.

+4
source share
1 answer

The problem may be that FindWindow already returning a top-level window (i.e. the one you entered Word inside). IMO there is no need to call GetAncestor , it always returns the desktop window ...

0
source

Source: https://habr.com/ru/post/1336281/


All Articles