Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting application title from a Word OLE application object

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);
like image 278
Andrew Avatar asked Oct 04 '11 18:10

Andrew


People also ask

Which of the following is the top level object in Word?

At the top of the hierarchy is the Application object. This object represents the current instance of Word. The Application object contains the Document, Selection, Bookmark, and Range objects. Each of these objects has many methods and properties that you can access to manipulate and interact with the object.

What do you call the bar located at the bottom right of the MS Word window which shows the current progress of a certain executed action in the program?

The taskbar is the access point for programs displayed on the desktop. With the new Windows 7 taskbar features, users can give commands, access resources, and view program status directly from the taskbar.

How do I make a Word document active?

Figure 4.13 You can make any open document active by choosing its name from the Switch Windows menu. Because every open Word document is represented by a taskbar button, you can also switch documents by clicking the appropriate taskbar button. To view all open documents at the same time, click Arrange All.


1 Answers

Is this what you are looking for?

How to obtain the window handle for an Office Automation server

like image 59
Jerry Gagnon Avatar answered Sep 22 '22 09:09

Jerry Gagnon