【问题标题】:MS Word automation with 2 way communication带有 2 路通信的 MS Word 自动化
【发布时间】:2011-03-08 19:01:26
【问题描述】:

我正在使用 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 无济于事。

【问题讨论】:

  • 也许我可以使用 SetWindowText 将正确的 hwnd 传递给 VBA

标签: c# winapi ms-word automation


【解决方案1】:

问题可能是,FindWindow 已经返回了一个顶级窗口(即您嵌入 Word 的那个窗口)。 IMO 无需调用GetAncestor,这总是返回桌面窗口...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    相关资源
    最近更新 更多