TOleContainer (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example uses an OLE container and a main menu on a form. When you click the Open or Save options in the main menu, an open/save dialog is displayed and, by default, you are 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.

Code

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);
}

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  OpenDialog1->Filter = "Documents (*.doc)|*.DOC|Any file (*.*)|*.*";
  SaveDialog1->Filter = "Documents (*.doc)|*.DOC|Any file (*.*)|*.*";
}

Uses