Getting application title from a Word OLE application object

空扰寡人 提交于 2019-12-09 12:46:36

问题


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:


Is this what you are looking for?

How to obtain the window handle for an Office Automation server



来源:https://stackoverflow.com/questions/7652171/getting-application-title-from-a-word-ole-application-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!