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.
source share