RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TOleContainer.SaveAsDocument Method

Saves the OLE object to the specified file in native OLE Document format.

Pascal
procedure SaveAsDocument(const FileName: string);
C++
__fastcall SaveAsDocument(const AnsiString FileName);

Call SaveAsDocument to save the OLE object in its native OLE Document format. An OLE object must already be loaded in the container before calling SaveAsDocument. Otherwise, SaveAsDocument raises an exception.

Note: Saving an OLE object to a file or a stream limits the ability of another application, such as Word, to open the file, even if it is saved with the correct extension. Saving to a file or a stream loses information necessary to open such a file. Use SaveAsDocument when needing to save files in a native OLE Document format.
 

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!