views:

331

answers:

2

I would like to be able to 'restore' MS Word's document window using Automation.

I already have the application object and I have tried calling App.Activate. Activate will bring the window to the top of the Window stack but not if Word is minimized. I can set the WindowState to Maximized, Minimized and Normal but what I really need to do is restore the window to the state it was in before the user minimized it (this would be equivalent to the user clicking on the application's button in the taskbar).

Is this possible using Word automation?

I am actually using C++ to to the automation so I am able to call Win32 API functions if required.

If there is not direct way to restore the window then the question becomes, 'how can I get the window handle of the ActiveWindow?'

A: 

Try ::ShowWindow(handle, SW_RESTORE)

Assaf Lavie
I have thought of this approach but the object model does not seem to supply a way to get the window handle, which I would need in order to call ::ShowWindow()
jmatthias
A: 

The best way I have come up with so far is as follows.

Get the active window using Application.ActiveWindow

This window object has a property named Caption. The caption appears in the document window's title.

I then use the Win32 function EnumWindows() to search for the Win32 window. I get the window text for each window returned by EnumWindows() using the Win32 function GetWindowText(). I attempt to find the caption in the window text and as an extra check I make sure the window text also contains the string 'Microsoft Word'.

Once I find the window, I then have it's Win32 window handle which I can then pass to the Win32 function ShowWindow(hWnd, SW_RESTORE).

You also have to call Application.Activate.

This solution is not perfect, for example I don't really like to assume that the window title will always contain the text 'Microsoft Word' but I also don't want to pick the wrong window.

jmatthias
You'd be better off using EnumWindows() but then for each window calling GetClassName, and then check for the class name "OpusApp" which identified it as a Word window.
Nick