以下是个示例:
uses
ComObj,Word_TLB,...;
...
procedure TForm1.Word1Click(Sender: TObject);
var
wordapp:WordApplication;
wordapp0,wenjian:OleVariant;
n,h,i,l:Byte;
begin
//连接到'模板.doc'
wordapp:=CreateOleObject('Word.Application') as WordApplication;
wordapp0:=wordapp;
wordapp.Visible:=True;
wenjian:=wordapp0.Documents.Open(ExtractFilePath(Application.ExeName)+'模板.doc',,,,'');
//将数据填入表格
h:=2;
i:=1;
for n:=1 to Self.StringGrid1.RowCountdo
begin
if h=2 then
begin
wenjian.Tables.Item(i).Cell(26,2).Range.insertafter(Self.LabeledEdit3.Text);
wenjian.Tables.Item(i).Cell(27,2).Range.insertafter(Self.LabeledEdit4.Text);
end;
for l:=1 to 8do
wenjian.Tables.Item(i).Cell(h,l).Range.insertafter(Self.StringGrid1.Cells[l,n]);
h:=h+1;
//超出表格就填写下一个表格
if h>25 then
begin
i:=i+1;
h:=2
end;
end;
end;