RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
ComObj.CreateOleObject Function

Instantiates an Automation object.

Pascal
function CreateOleObject(const ClassName: string): IDispatch;
C++
IDispatch CreateOleObject(const AnsiString ClassName);

CreateOleObject creates a single uninitialized object of the class specified by the ClassName parameter. ClassName specifies the string representation of the Class ID (CLSID). CreateOleObject is used to create an object of a specified type when the CLSID is known, and when the object is on a local or in-proc server. Only objects that are not part of an aggregate are created using CreateOleObject.

Note: In Delphi code, CreateOleObject is called once to create each new single instance of a class. To create multiple instance of the same class, using a class factory is recommended.
CreateOleObject returns a reference to the interface that can be used to communicate with the object. For CreateOleObject this interface is of type IDispatch. To create a COM object that does not support an IDispatch interface, use CreateComObject.
Tip: As shown in the example, if the interface returned by CreateOleObject is assigned to a Variant, you can release the interface by assigning the Unassigned constant to that Variant.
 

C++ Examples: 

 

/*
The following code shows an example of how to create an Ole Object and how to
perform specific operations.
*/
void __fastcall TMyForm::ButtonClick(TObject *Sender)
{
    OleVariant WordApp, NewDoc;

    /* Creates a Microsoft Word application. */
    WordApp = CreateOleObject("Word.Application");

    /* Creates a new Microsoft Word document. */
    NewDoc = WordApp.OlePropertyGet("Documents").OleFunction("Add");

    /* Inserts the text 'Hello World!' in the document. */
    WordApp.OlePropertyGet("Selection").OleFunction("TypeText", "Hello World!");

    /* Saves the document on the disk. */
    NewDoc.OleFunction("SaveAs", "my_new_document.doc");

    /* Closes Microsoft Word. */
    WordApp.OleFunction("Quit");

    /* Releases the interface by assigning the Unassigned constant to the Variant variables. */
    NewDoc = Unassigned;
    WordApp = Unassigned;
}

 

Delphi Examples: 

{
The following code shows an example of how to create an Ole Object and how to
perform specific operations.
}
procedure TForm1.ButtonClick(Sender: TObject);
var
  WordApp, NewDoc: Variant;
begin
  { Creates a Microsoft Word application. }
  WordApp := CreateOleObject('Word.Application');
  { Creates a new Microsoft Word document. }
  NewDoc := WordApp.Documents.Add;
  { Inserts the text 'Hello World!' in the document. }
  WordApp.Selection.TypeText('Hello World!');
  { Saves the document on the disk. }
  NewDoc.SaveAs('my_new_document.doc');
  { Closes Microsoft Word. }
  WordApp.Quit;
  { Releases the interface by assigning the Unassigned constant to the Variant variables. }
  NewDoc := Unassigned;
  WordApp := Unassigned;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!