用WordDocument和WordApplication。
例:
procedure TForm1.Button1Click(Sender: TObject);
var
Template, NewTemplate, DocumentType, Visible, ItemIndex: OleVariant;
procedure setfont;
begin
WordFont1.ConnectTo(WordDocument1.Sentences.Get_Last.Font);
WordFont1.Bold := 1;
WordFont1.Italic := 1;
WordFont1.shadow := 1;
WordFont1.Size := 12;
end;
begin
try
Template := EmptyParam;
NewTemplate := True;
ItemIndex := 1;
try
Wordapplication1.Connect;
except
MessageDlg('无法连接,也许没有安装Word', mtError, [mbOk], 0);
Abort;
end;
Wordapplication1.Visible := False;
WordApplication1.Caption := 'Delphi Word';
//建立一个新文档
Template := EmptyParam;
NewTemplate := False;
WordApplication1.Documents.Add(Template, NewTemplate,DocumentType, Visible);
//建立WordDocument连接
WordDocument1.ConnectTo(WordApplication1.Documents.Item(ItemIndex));
//因为Word进行拼写检查需要很多时间,所以首先关闭检查
WordApplication1.Options.CheckSpellingAsYouType := False;
WordApplication1.Options.CheckGrammarAsYouType := False;
//将Memo1的内容拷贝到Word中
SetFont;
WordDocument1.Range.InsertAfter('Memo1的文本: '+#13+Memo1.Text+#13);
Button2.Enabled := True;
Button3.Enabled := True;
Button1.Enabled := False;
except
on E: Exception do
begin
ShowMessage(E.Message);
WordApplication1.Disconnect;
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
WordDocument1.PrintOut;
end;