Seena P
Seena P

Reputation: 23

Delphi Ms word Automation Page setup and save in windows 10

How it possible to setup pages like legal, A4 etc in Delphi Word automation - after CreateOleObject('Word.Application') and save the delphi created word documents with a specific name in C drive .

Upvotes: 1

Views: 1156

Answers (1)

MartynA
MartynA

Reputation: 30715

The code below will create a document with a specified papersize and save it under a specified name:

uses ... Word2000;

procedure TForm1.CreateDocWithPaperSize;
var
  MSWord,
  Document,
  PageSetUp: OleVariant;
  AFileName : String;
  iDocument : WordDocument;
begin
  MsWord := CreateOleObject('Word.Application');
  MsWord.Visible := True;

  Document := MSWord.Documents.Add;
  MSWord.Selection.Font.Size := 22;
  MSWord.Selection.Font.Bold := true;
  MSWord.Selection.TypeText(#13#10);

  // the following is to get the WordDocument interface 'inside' the
  //  Document variant, so that we can use code completion on
  // iDocument in the IDE to inspect its properties
  iDocument := IDispatch(Document) as WordDocument;

  PageSetUp := iDocument.PageSetup;
  PageSetUp.PaperSize := wdPaperLegal;
  MSWord.Selection.TypeText('Hello Word.');

  AFileName := 'C:\Temp\Test.Docx';
  Document.SaveAs(FileName := AFileName);
end;

Word2000.Pas is an import unit of the MS Word type library (there are other versions - see the Servers subfolder under the OCX folder in your Delphi set-up). In it, search for

wdPaperSize

, which you will find declared as a TOleEnum. Immediately below that you will find a list of constants that let you specify particular paper sizes.

{ From Word2000.Pas }
// Constants for enum WdPaperSize
type
  WdPaperSize = TOleEnum;
const
  wdPaper10x14 = $00000000;
  wdPaper11x17 = $00000001;
  wdPaperLetter = $00000002;
  wdPaperLetterSmall = $00000003;
  wdPaperLegal = $00000004;
  wdPaperExecutive = $00000005;
  wdPaperA3 = $00000006;
  wdPaperA4 = $00000007;
  wdPaperA4Small = $00000008;
  wdPaperA5 = $00000009;
  wdPaperB4 = $0000000A;
  wdPaperB5 = $0000000B;
  wdPaperCSheet = $0000000C;
  // etc

Upvotes: 4

Related Questions