发给你几个例子,以供参考[Delphi 5+office97] :
procedure TFormExecQuery3.ToWord();
var
Bookmark: TBookmark;
RangeW: Word2000.Range;
v1: Variant;
ov1: OleVariant;
Row1: Word2000.Row;
Template,NewTemplate,ItemIndex:OleVariant;
row,col:integer;
procedure Senddata();
var
I:integer;
tempstr:string;
begin
RangeW:=WordDocument1.Range;
With Query1 do
begin
// disable the UI
DisableControls;
Try
// 加入字段标题
tempstr:='';
for I := 0 to Fields.Count - 2do
tempstr:= tempstr+Fields .DisplayLabel+#9;
tempstr:= tempstr+Fields .DisplayLabel;
WordDocument1.Range.InsertParagraphAfter;
RangeW:=WordDocument1.Paragraphs.Last.Range;
RangeW.Text := tempstr;
// RangeE := RangeE.Next;
First;
row:=RangeW.End_ +1;
col:=Fields.Count ;
while not EOFdo
begin
// 加入字段数据
WordDocument1.Range.InsertParagraphAfter;
RangeW:=WordDocument1.Paragraphs.Last.Range;
tempstr:='';
for I := 0 to Fields.Count - 2do
tempstr:= tempstr+Fields .AsString+#9 ;
tempstr:= tempstr+Fields .AsString;
RangeW.Text := tempstr;
inc(row) ;
Next;
end;
finally
// re-enable the controls
EnableControls;
end;
end;
end;
begin
try
Template := EmptyParam;
NewTemplate := True;
ItemIndex := 1;
try
Wordapplication1.Connect;
//打开Word应用程序
except
MessageDlg('Word 97 没有安装', mtError, [mbOk], 0);
Abort;
end;
Wordapplication1.Visible := True;
//使Word应用程序可见
WordApplication1.Caption := 'OLE自动化范例';
//Create newdo
cument
Template := EmptyParam;
NewTemplate := False;
// WordApplication1.Documents.Add(Template, NewTemplate);
//指定一个Word文档
WordDocument1.Activate;
WordDocument1.ConnectTo (WordApplication1.Documents.Item(ItemIndex) );
// 插入报表标题
WordDocument1.Range.Text := '报表' ;
WordDocument1.Range.Font.Size := 14;
//发送数据到WordDocument1
Senddata();
//把数据转化为表格形式
RangeW := WordDocument1.Content;
v1 := RangeW;
row:=row-1;
v1.ConvertToTable (#9, row, col);
//设置标题格式:实体18号字居中
Row1 := WordDocument1.Tables.Item(1).Rows.Get_First;
//Row1:=Row1.Next ;
Row1.Range.Bold := 1;
Row1.Range.Font.Size := 18;
Row1.Range.ParagraphFormat.Alignment :=wdAlignParagraphCenter;
Row1.Range.InsertParagraphAfter;
ov1 := ' ';
//取消标题表格
Row1.ConvertToText (ov1);
Finally
//关闭Word应用程序Wordapplication1
Wordapplication1.Disconnect ;
end;
end;