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.

6
  • How do you retrieve the window handle? Note that your question may be a duplicate of stackoverflow.com/questions/9099479/… Commented Jan 12, 2017 at 8: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
    Commented Jan 13, 2017 at 2: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
    Commented Jan 13, 2017 at 2: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. Commented Jan 13, 2017 at 7: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
    Commented Jan 17, 2017 at 3:14

0

Browse other questions tagged or ask your own question.