RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TOleContainer.CreateObjectFromFile Method

Creates an embedded OLE object from the contents of a file.

Pascal
procedure CreateObjectFromFile(const FileName: string; Iconic: Boolean);
C++
__fastcall CreateObjectFromFile(const AnsiString FileName, Boolean Iconic);

Call CreateObjectFromFile to create an OLE object from the file specified by the FileName parameter. The Iconic parameter indicates whether the object is shown as an icon (true) or displayed as it would be in the server application (false). If there's already an OLE object in the container, it is destroyed and any changes the user made to it are discarded.  

C++ Examples: 

 

/*
This example uses an OLE container and a main menu on a form.
When the user clicks the Open or Save options in the main menu,
an open/save dialog is displayed and by default,
the user is required to enter the name of a Word document.
Files with other extensions supported by the OLE container
can also be accessed. An additional test is made to assure that
the file to open exists and that no file overwriting is
going to take place.
*/
void __fastcall TForm1::Exit1Click(TObject *Sender)
{
  Close();
}

void __fastcall TForm1::Open1Click(TObject *Sender)
{
  if (OpenDialog1->Execute())
    if (FileExists(OpenDialog1->FileName))
      OleContainer1->CreateObjectFromFile(OpenDialog1->FileName, false);
    else
      throw(Exception("File not found."));
}

void __fastcall TForm1::Save1Click(TObject *Sender)
{
  if (SaveDialog1->Execute())
    if (FileExists(SaveDialog1->FileName))
      throw(Exception("File already exists. Cannot overwrite."));
  else
    OleContainer1->SaveAsDocument(SaveDialog1->FileName);
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  OpenDialog1->Filter = "Documents (*.doc)|*.DOC|Any file (*.*)|*.*";
  SaveDialog1->Filter = "Documents (*.doc)|*.DOC|Any file (*.*)|*.*";
}

 

Delphi Examples: 

{
This example uses an OLE container and a main menu on a form.
When the user clicks the Open or Save options in the main menu,
an open/save dialog is displayed and by default,
the user is required to enter the name of a Word document.
Files with other extensions supported by the OLE container
can also be accessed. An additional test is made to assure that
the file to open exists and that no file overwriting is
going to take place.
}
procedure TForm1.Exit1Click(Sender: TObject);
begin
  Close;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  OpenDialog1.Filter := 'Documents (*.doc)|*.DOC|Any file (*.*)|*.*';
  SaveDialog1.Filter := 'Documents (*.doc)|*.DOC|Any file (*.*)|*.*';
end;

procedure TForm1.Open1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    if FileExists(OpenDialog1.FileName) then
      OleContainer1.CreateObjectFromFile(OpenDialog1.FileName, false)
    else
      raise Exception.Create('File not found.');
end;

procedure TForm1.Save1Click(Sender: TObject);
begin
  if SaveDialog1.Execute then
    if FileExists(SaveDialog1.FileName) then
      raise Exception.Create('File already exists. Cannot overwrite.')
    else
      OleContainer1.SaveAsDocument(SaveDialog1.FileName);
end;

 

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