2011-01-20 43 views
4

我正在使用Win32 SetParent函數在我的應用程序中嵌入MS Word。 一切工作正常,但有一個要求從Word自定義 工具欄按鈕回調到父應用程序。 Word實例嵌入在用戶控件中,所以 父是this.Handle。雙向溝通的MS Word自動化

的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無濟於事。

+0

也許我可以使用SetWindowText爲了將正確的hwnd傳遞給VBA – SparcU 2011-01-20 10:03:46

回答

0

問題可能是,FindWindow已經返回一個頂級窗口(即嵌入Word的窗口)。國際海事組織沒有必要撥打GetAncestor,這總是返回桌面窗口...