Author: Jonas Bilinkevicius
Is is possible to create a table in Word via OLE Automation and to specify the
value of each cell?
Answer:
Yes. If Doc is a TWordDocument, for example:
1 { ... }
2 var
3 Tbl: Table;
4 R: Range;
5 Direction: OleVariant;
6 { ... }
7 Direction := wdCollapseEnd;
8 R := Doc.Range;
9 R.Collapse(Direction);
10 Tbl := Doc.Tables.Add(R, 2, 4, EmptyParam, EmptyParam);
11 Tbl.Cell(1, 1).Range.Text := 'Row 1, Col 1';
12 Tbl.Cell(1, 2).Range.Text := 'Row 1, Col 2';
13
14 //But doing things with individual table cells in Word is extremely slow. If you
15 can, it's better to enter the data as (for example) comma-separated values and
16 convert it into a table only as the last step. Here'
17
18 { ... }
19 const
20 Line1 = 'January,February,March';
21 Line2 = '31,28,31';
22 Line3 = '31,59,90';
23 var
24 R: Range;
25 Direction, Separator, Format: OleVariant;
26 { ... }
27 R := Word.Selection.Range;
28 Direction := wdCollapseEnd;
29 R.Collapse(Direction);
30 R.InsertAfter(Line1);
31 R.InsertParagraphAfter;
32 R.InsertAfter(Line2);
33 R.InsertParagraphAfter;
34 R.InsertAfter(Line3);
35 R.InsertParagraphAfter;
36 Separator := ',';
37 Format := wdTableFormatGrid1;
38 R.ConvertToTable(Separator, EmptyParam, EmptyParam, EmptyParam, Format, EmptyParam,
39 EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
40 EmptyParam, EmptyParam);
41 { ... }
|