2

I would like to query the display name of an OLE application, which is embedded in my Delphi XE4 Win32 app.

TOleContainer class is used and the container can contain different OLE applications (e.g. MS Word, MS Excel, ...), dependent on what file is edited within.

What I want to be returned is Microsoft Word 2007 (or 2010 or 2013 or ...) or at least Microsoft Word, as it is displayed in the title bar of a normal Word instance.


EDIT: TOndrej's answer was very helpful. Thank you.

Unfortunally, as described in my comment underneath his answer, it does not show the real application caption. I found this question on SO. It's said there, that I could access the host application's caption with _Application.Caption property. I don't have an instance of _Application but IOleObject. Typecast (MyOleObjectInterface as _Application) failed.

How can the OleObject be accessed as _Application?

Community
  • 1
  • 1
René Hoffmann
  • 2,766
  • 2
  • 20
  • 43

1 Answers1

6

See IOleObject.GetUserType method:

function GetOleObjectAppName(const OleObject: IOleObject): string;
var
  AppName: PWideChar;
begin
  OleCheck(OleObject.GetUserType(USERCLASSTYPE_APPNAME, AppName));
  try
    Result := AppName;
  finally
    CoTaskMemFree(AppName);
  end;
end;

Usage example:

  ShowMessage(GetOleObjectAppName(OleContainer1.OleObjectInterface));
Ondrej Kelle
  • 36,941
  • 2
  • 65
  • 128
  • Thank you very much. But: It seems to return the name based on the current file format. For example, it returns *Microsoft Excel 2003* for a Excel97-2003 file, although Microsoft Office 2007 is installed. Is there a way to avoid that and return the name of the actual handler? – René Hoffmann Jul 30 '15 at 16:18
  • btw: is `try..finally` used correctly here? If `OleCheck` raises an exception, memory for `AppName` wouldn't be freed, right? – René Hoffmann Jul 30 '15 at 16:19
  • 1
    @RenéHoffmann Yes and that's the correct usage. If `Olecheck` raises an exception nothing is allocated, nothing is freed. – Ondrej Kelle Jul 30 '15 at 17:02
  • 2
    Perhaps you could start with [`IOleObject.GetUserClassID`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682313(v=vs.85).aspx) instead and look up the application name in the [registry](http://www.codeproject.com/Articles/1265/COM-IDs-Registry-keys-in-a-nutshell), (possibly via `AppID`). – Ondrej Kelle Jul 30 '15 at 17:42