0

I'm developing a addin for office word using vsto. I'm trying to bring the word window that is minisized rebringing it to the front.I try to use winapi function SwitchToThisWindowSetForegroundWindow, SetWindowPos like this

if (handlePtr != IntPtr.Zero)
{
    Win32API.SwitchToThisWindow(handlePtr, true);
    //Win32API.ShowWindow(handlePtr, Win32API.SW_MAXIMIZE);
    ////Win32API.SetForegroundWindow(handlePtr); 
    //Win32API.SetWindowPos(handlePtr, Win32API.HWND_TOP, 0, 0, 0, 0, Win32API.SWP_NOMOVE | Win32API.SWP_NOSIZE | Win32API.SWP_SHOWWINDOW);
}

and Application.Activate(), it works how every other function like this does and only makes it flash in taskbar instead of bringing it to the front.

Is there anyway to fix this? I also try to sendkeys ‘alt+tab’ but i don't know how to direct the word process window.

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
asa9891
  • 3
  • 4
  • How do you retrieve the window handle? Note that your question may be a duplicate of http://stackoverflow.com/questions/9099479/restore-a-minimized-window-of-another-application – Dirk Vollmar Jan 12 '17 at 08:05
  • You are right,i get the main window handle is by getting current process's MainWindowHandle.I think it had changed when is minimized.But the question is how can i get word's mainwindowhandle when word addin's Ribbon_Load function is excuting. – asa9891 Jan 13 '17 at 02:46
  • I have to try to call winapi findow("NetUIHWND",null) and get result is 0.The class NetUIHWND is by using spy++ to get. – asa9891 Jan 13 '17 at 02:59
  • The `MainWindowHandle` can easily cause problems: it may no longer be valid, e.g. if the first document is closed, or you get the wrong window if several documents are open. It is better to use the `Window.Hwnd` property, e.g. via `Application.ActiveDocument.Window[1].Hwnd` or `Application.ActiveWindow`. – Dirk Vollmar Jan 13 '17 at 07:09
  • Thanks,I fund a another way of working. this is my code: `WordApplication.Activate(); WordApplication.Visible = true; WordApplication.ActiveDocument.Activate(); IntPtr winfrom = IntPtr.Zero; if (GetNEClientRunningState(out winfrom)) { Win32API.SwitchToThisWindow(winfrom, true); System.Threading.Thread.Sleep(30); SendKeys.SendWait("%{TAB}"); } ` – asa9891 Jan 17 '17 at 03:14
  • Hi dirk,i get another serious trouble,it can not active the last active document when opening many word document. I have try to use your suggest code `Application.ActiveDocument.Window[1].Hwnd` and `Application.ActiveWindow`.Sorry for the inconvenience! This is my code `IntPtr handlePtr = new IntPtr(WordApplication.ActiveDocument.Windows[1].Hwnd);Win32API.SwitchToThisWindow(handlePtr, true);Win32API.ShowWindow(handlePtr, Win32API.SW_MAXIMIZE);Win32API.SetForegroundWindow(handlePtr)` – asa9891 Jan 17 '17 at 07:23

0 Answers0