[英]MS Word automation with 2 way communication
我正在使用Win32 SetParent函数在我的应用程序中嵌入MS Word。 一切正常,但需要从Word自定义工具栏按钮到父应用程序进行回调。 Word实例嵌入在用户控件中,因此父级是this.Handle。
VBA代码如下所示:
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
在C#中,代码是这样的:
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);
}
问题似乎是VBA无法找到正确的窗口句柄。 我尝试使用GetParent无济于事。
问题可能是, FindWindow
已经返回一个顶级窗口(即嵌入Word的窗口)。 IMO没有必要调用GetAncestor
,这总是返回桌面窗口...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.