Mega Code Archive

 
Categories / Delphi / Files
 

Activating an Object from a File

Title: Activating an Object from a File Question: Many OLE Automation applications allow the user to save objects in files. For example, a spreadsheet application that supports Worksheet objects allows the user to save the worksheet in a file. The same application may also support a chart object that the user can save in a file. To activate an object that has been saved in a file, you use the GetObject function. Answer: uses ComObj, ActiveX, UrlMon; // GetObject function function GetObject(UserName: string): IUnknown; var BindCtx: IBindCtx; Mk: IMoniker; chEaten: ULONG; begin OleCheck(CreateBindCtx(0, BindCtx)); OleCheck(MkParseDisplayNameEx(BindCtx, PWideChar(WideString(UserName)), chEaten, Mk)); OleCheck(Mk.BindToObject(BindCtx, nil, IUnknown, Result)); end; // Sample Code procedure TForm1.FormCreate(Sender: TObject); var Document: OleVariant; Application: OleVariant; begin // create object and get dispatch interface Document := GetObject('c:\My Documents\Demo.doc') as IDispatch; // get application object Application := Document.Application; // show application window Application.Visible := true; end;