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 SwitchToThisWindow
、SetForegroundWindow
, 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.
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 theWindow.Hwnd
property, e.g. viaApplication.ActiveDocument.Window[1].Hwnd
orApplication.ActiveWindow
.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}"); }