7
votes

Is there a way of getting the window title from a Word.Application OLE object? I'd like to use it to try get the window using FindWindow.

I'm creating an OLE object and adding an existing document, like so:

App := CreateOLEObject('Word.Application');
App.Visible := True;
App.Activate;
Doc := App.Documents.Open('File.doc');

At this point, the caption values are the following:

App.Caption => 'Microsoft Word'
Doc.ActiveWindow.Caption => 'File.doc [Compatibility Mode]'

However, the window title is actually File.doc [Compatibility Mode] - Microsoft Word.

Is there some way of getting this window title from the OLE object (there does not seem to be a better way of getting the HWND itself without using FindWindow)? I doubt it is safe to assume the window title will always be <doc caption> - <app caption>.

I'd like to use the FindWindow function to get a handle to the window to be able to bring it to the foreground (see OLE Automation to launch MS Word and bring to front) in a slightly safer manner by passing in the correct title.

EDIT

Here's the fix using the workaround described in http://support.microsoft.com/kb/258511

App := CreateOLEObject('Word.Application');

// get the handle
TempTitle := 'Temp - ' + IntToStr(Random(1000000));
App.Caption := TempTitle;
Handle := FindWindow('OpusApp', PWideChar(TempTitle));
App.Caption := EmptyStr;

App.Visible := True;
App.Activate;
Doc := App.Documents.Open('File.doc');

// bring to front
SetForegroundWindow(Handle);
1

1 Answers