2007. november 5., hétfő

Insert a row into an existing table in a Word document

Problem/Question/Abstract:

I want to add rows to an existing table but have difficulties using InsertRows. Does anyone have an example?

Answer:

uses
ComObj;

procedure TForm1.Button1Click(Sender: TObject);
var
App, WordDoc, tabelle: OLEVariant;
begin
try
{Create MSWord instance}
App := CreateOleObject('Word.Application');
except
{Error...}
Exit;
end;
{Open a Word Document}
WordDoc := App.Documents.Open('c:\test.doc');
{Insert a table}
tabelle := WordDoc.Tables.Add(App.Selection.Range, 5 {Columns}, 4 {Rows});
{Write sth into a cell}
tabelle.Cell(2 {Column}, 4 {Row}).Range.Text := '123';
{Append row to table}
App.Selection.Tables.Item(1).Rows.Item(App.Selection.Tables.Item(1).Rows.Count).Select;
App.Selection.InsertRowsBelow;
{Set Column width}
App.Selection.Tables.Item(1).Columns.Item(1).SetWidth(ColumnWidth := 40,
RulerStyle := $00000000 {wdAdjustNone});
{Show MSWord}
App.Visible := True;
{Cleanup...}
App := Unassigned;
WordDoc := Unassigned;
tabelle := Unassigned;
end;


Nincsenek megjegyzések:

Megjegyzés küldése