Comparison between native oleWord implementation and TWordApplication / VCL classes implementation


//-------------- native oleWord implementation ---------------
//---------------------------------------------------------------------------

#include
#include
#include

#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
int C;
AnsiString FileName;

FileName="essai.doc";

Variant vMSWord, vWDocument, vWDocuments;
// Lance Word ou se connecte à un word existant
try
{
vMSWord = Variant::GetActiveObject("Word.Application");
} catch(...)
{
vMSWord = Variant::CreateObject("Word.Application");
}
vMSWord.OlePropertySet("Visible", true);
// cree un nouveau doc
vWDocuments = vMSWord.OlePropertyGet("Documents");
vWDocument = vWDocuments.OleFunction("Add");
vWDocument.OleProcedure("Saveas", FileName.c_str()); // ici on sauve un document

// on prepare la Font du texte
vMSWord.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Name", "Verdana");
vMSWord.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Size", "8");
// on insere du texte
vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "Calendrier");
// on fait un passage a la ligne suivante
vMSWord.OlePropertyGet("Selection").OleFunction("TypeParagraph");

Variant Table1, MyRange, MyRangeS, MyRangeE;
// table du calendrier
// on insere un tableau 8 lignes 24 colonnes
vWDocument.OlePropertyGet("Tables").\
OleFunction("Add", vMSWord.OlePropertyGet("Selection").OlePropertyGet("Range"), 8, 24);
Table1 = vWDocument.OlePropertyGet("Tables").OleFunction( "Item", 1 );
Table1.OleFunction( "Cell", 1, 1 ).OlePropertyGet( "Range" ).\
OleFunction("InsertAfter", "Janvier");
Table1.OleFunction( "Cell", 2, 1 ).OlePropertyGet( "Range" ).OleFunction("InsertAfter", "L");
Table1.OleFunction( "Cell", 2, 2 ).OlePropertyGet( "Range" ).OleFunction("InsertAfter", "M");
Table1.OleFunction( "Cell", 2, 3 ).OlePropertyGet( "Range" ).OleFunction("InsertAfter", "M");
Table1.OleFunction( "Cell", 2, 4 ).OlePropertyGet( "Range" ).OleFunction("InsertAfter", "J");
Table1.OleFunction( "Cell", 2, 5 ).OlePropertyGet( "Range" ).OleFunction("InsertAfter", "V");
Table1.OleFunction( "Cell", 2, 6 ).OlePropertyGet( "Range" ).OleFunction("InsertAfter", "S");
Table1.OleFunction( "Cell", 2, 7 ).OlePropertyGet( "Range" ).OleFunction("InsertAfter", "D");

Table1.OleFunction("Cell", 2, 1).OleFunction("Select");
vMSWord.OlePropertyGet("Selection").OleFunction("EndOf", wdColumn, wdExtend);
vMSWord.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Bold",true);

MyRange = vWDocument.OleFunction("Range", Table1.OleFunction("Cell", 1, 1)
.OlePropertyGet("Range").OlePropertyGet("Start"), Table1.OleFunction("Cell", 1, 3).OlePropertyGet("Range").OlePropertyGet("End"));
MyRange.OlePropertyGet("Font").OlePropertySet("Bold",true);
MyRange.OlePropertyGet("Cells").OleProcedure("Merge");

// une autre maniere
Table1.OleFunction("Cell", 1, 1).OleFunction("Merge", Table1.OleFunction("Cell", 1, 3));

Table1.OlePropertyGet("Rows").OleFunction("Item", "2").OleFunction("Select");
vMSWord.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Bold",true);

// fin de fichier
vMSWord.OlePropertyGet("Selection").OleProcedure("EndKey", "6");
// on fait un passage a la ligne suivante
vMSWord.OlePropertyGet("Selection").OleFunction("TypeParagraph");
vMSWord.OlePropertyGet("Selection").OleFunction("TypeParagraph");

vMSWord.OlePropertyGet("Selection").OlePropertyGet("ParagraphFormat").OlePropertySet("Alignment", wdAlignParagraphCenter);
vMSWord.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Name", "Arial");
vMSWord.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Size", "20");
vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "Nouveau titre Arial 20");
vMSWord.OlePropertyGet("Selection").OleFunction("TypeParagraph");

vMSWord.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Name", "Times");
vMSWord.OlePropertyGet("Selection").OlePropertyGet("ParagraphFormat").OlePropertySet("Alignment", wdAlignParagraphJustify);
vMSWord.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Size", "10");
vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "Nouveau titre Times 10");
vMSWord.OlePropertyGet("Selection").OleFunction("TypeParagraph");
vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "Ceci est une phrase Times 10");

vMSWord.OlePropertyGet("Selection").OleFunction("InsertBreak", wdPageBreak);
vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "Page 2");
vMSWord.OlePropertyGet("Selection").OleFunction("TypeParagraph");
vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "Page 2 2eme paraghraphe");


//"exp_masde.jpg"

vWDocument.OleProcedure("Save"); // ici on sauve un document
Application->MessageBoxA("Cliquez pour fermer", "Debug", MB_OK);
vWDocument.OleFunction("Close"); // on ferme le document
// vMSWord.OleFunction("Quit"); // on ferme Word
vMSWord = Unassigned; // on libere les Variants

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Form1->Close();
}
//---------------------------------------------------------------------------

//-------------- TWordApplicatrion / VCL implementation ---------------
//---------------------------------------------------------------------------

#include
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Word_XP_srvr"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
int C;
Variant FileName;
Table * Table1;
WordRangePtr MyRange;

FileName="essai.doc";
// Lance Word ou se connecte à un word existant
WordApplication1->Connect();
WordApplication1->Visible=true;
// Cree un fichier Word
WordDocument1->Connect();

WordDocument1->SaveAs( FileName); // ici on sauve un document
// on prepare la Font du texte
WordApplication1->Selection->Font->Name=WideString("Verdana");
WordApplication1->Selection->Font->Size=8;
// on insere du texte
WordApplication1->Selection->TypeText(WideString("Calendrier"));
// on fait un passage a la ligne suivante
WordApplication1->Selection->TypeParagraph();
// table du calendrier
// on insere un tableau 8 lignes 24 colonnes
Table1=WordDocument1->Tables->AddOld( WordApplication1->Selection->Range, 8, 24);
Table1->Cell(1,1)->Range->InsertAfter(WideString("Janvier"));
Table1->Cell(2,1)->Range->InsertAfter(WideString("L"));
Table1->Cell(2,2)->Range->InsertAfter(WideString("M"));
Table1->Cell(2,3)->Range->InsertAfter(WideString("M"));
Table1->Cell(2,4)->Range->InsertAfter(WideString("J"));
Table1->Cell(2,5)->Range->InsertAfter(WideString("V"));
Table1->Cell(2,6)->Range->InsertAfter(WideString("S"));
Table1->Cell(2,7)->Range->InsertAfter(WideString("D"));

Table1->Cell(2,1)->Select();
WordApplication1->Selection->EndOf( (OleVariant)wdColumn, (OleVariant)wdExtend);
WordApplication1->Selection->Font->Bold=true;

MyRange->SetRange( Table1->Cell(1,1)->Range->Start, Table1->Cell(1,1)->Range->End);
MyRange->Font->Bold=true;
MyRange->Cells->Merge();
// une autre maniere
Table1->Cell(1,1)->Merge( Table1->Cell(1, 3));

Table1->Rows->Item(2)->Select();
WordApplication1->Selection->Font->Bold=true;

// on fait un passage a la fin du document
WordApplication1->Selection->EndKey((OleVariant)wdStory, (OleVariant)wdMove);
WordApplication1->Selection->TypeParagraph();
WordApplication1->Selection->TypeParagraph();

WordApplication1->Selection->ParagraphFormat->Alignment=wdAlignParagraphCenter;
WordApplication1->Selection->Font->Name=WideString("Arial");
WordApplication1->Selection->Font->Size=20;
WordApplication1->Selection->TypeText(WideString("Nouveau titre Arial 20"));
WordApplication1->Selection->TypeParagraph();

WordApplication1->Selection->Font->Name=WideString("Times");
WordApplication1->Selection->ParagraphFormat->Alignment=wdAlignParagraphJustify;
WordApplication1->Selection->Font->Size=10;
WordApplication1->Selection->TypeText(WideString("Nouveau titre Times 10"));
WordApplication1->Selection->TypeParagraph();
WordApplication1->Selection->TypeText(WideString("Ceci est une phrase Times 10"));

WordApplication1->Selection->InsertBreak( (OleVariant)wdPageBreak);
WordApplication1->Selection->TypeText(WideString("Page 2"));
WordApplication1->Selection->TypeParagraph();
WordApplication1->Selection->TypeText(WideString("Page 2 2eme paraghraphe"));

//"exp_masde.jpg"

WordDocument1->Save(); // ici on sauve un document
Application->MessageBoxA("Cliquez pour fermer", "Debug", MB_OK);
WordDocument1->Close(); // on ferme le document
// WordApplication1->Quit(); // on ferme Word
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Form1->Close();
}
//---------------------------------------------------------------------------