Mega Code Archive

 
Categories / Delphi / Examples
 

Work by WordApplication Component

Title: Work by WordApplication Component Question: WordApplication type into MSWord print MSWord document Answer: // Writer Ali Ebrahimi Dorcheh (iran,isfahan) unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, OleServer, WordXP; type TForm1 = class(TForm) Button1: TButton; WordApplication1: TWordApplication; Button2: TButton; procedure Button2Click(Sender: TObject); procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; created : Boolean; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin //show Window MSWord WordApplication1.Visible := True; created := True; //Create New File (Blank File) WordApplication1.Documents.Add(EmptyParam , EmptyParam , EmptyParam ,EmptyParam); //Page Setup with WordApplication1.ActiveDocument.PageSetup do begin TopMargin := 2.54 * 28.35; //1 cm = 28.35 points BottomMargin := 3 * 28.35; // 3 cm LeftMargin := 3.17 * 28.35 ; // 3.17 cm RightMargin := 3 * 28.35; PageWidth := 21 * 28.35; PageHeight := 29.7 * 28.35; end; //type into word with WordApplication1.Selection do begin LtrPara; //left to right //RtlPara for farsi and arabic languages Font.Name := 'Tahoma'; Font.Bold := wdToggle; //start Bold Font Font.BoldBi := wdToggle; //start Bold Font Font.Size := 14; TypeText('Hello'); //Type text into file Font.Bold := wdToggle; //end of bold Font Font.BoldBi := wdToggle; //end of bold Font TypeParagraph; //Enter to new paraghraph Font.Italic := wdToggle; //start Italic Font Font.ItalicBi := wdToggle; //start Italic Font TypeText('How are you?'); Font.Italic := wdToggle; //end Italic Font Font.ItalicBi := wdToggle; //end Italic Font end; end; procedure TForm1.Button2Click(Sender: TObject); begin Button2.Width := 120; Button2.Caption := 'Print'; if created = True then WordApplication1.PrintOut; end; procedure TForm1.FormCreate(Sender: TObject); begin Button1.Width := 120; Button1.Caption := 'New MSWord File'; end; end.