导出文字和图片到WORD

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,ComObj, WordXP, OleServer, ComCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
SaveDialog1: TSaveDialog;
RichEdit1: TRichEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
WordApp: TWordApplication;
WordDoc: TWordDocument;
Template,NewTemplate,DocumentType,Visible:OleVariant;
itemIndex:OleVariant;
fileName:Olevariant;
NoPrompt,OriginalFormat:OleVariant;
RouteDocument,SaveChanges:OleVariant;
LinkToFile, SaveWithDocument:OleVariant;
begin
//指定文档的路径和文件名
savedialog1.FileName :='';
if not savedialog1.Execute then exit;
filename:=savedialog1.FileName ;
//如果该日志的对应Word文档已经存在则提示是否覆盖
if FileExists(fileName)=true then
begin
Beep;
if Application.MessageBox('文档已经存在,是否覆盖?','警告',MB_OKCANCEL)=IDCANCEL then
exit;
end;

try
//连接到Word 2000
WordApp:=TWordApplication.Create(self);
WordApp.Connect ;
except
Beep;
MessageDlg('不能生成文档,请确认是否安装了Word !',mtError,[mbOK],0);
exit;
end;
//显示Word 2000

//给调用Add函数使用的实参赋值
Template:=EmptyParam;
NewTemplate:=False;
DocumentType:=wdNewBlankDocument;
Visible:=true;
//调用Add函数
WordApp.Documents.Add(Template,NewTemplate,DocumentType,Visible);
//连接到新建的文档
itemIndex:=1;
WordDoc:=tworddocument.Create(self);
WordDoc.ConnectTo(WordApp.Documents.Item(itemIndex));
//文档另存为
WordDoc.SaveAs(fileName);
//开始向Word文档中写入内容
with WordApp.Selection do
begin
Font.Size:=20;
Font.Bold:=2;
Paragraphs.Alignment:=wdAlignParagraphCenter;
TypeText('互联网代收费业务');
TypeParagraph; //换行
TypeText('订购流程');
TypeParagraph;
TypeParagraph;
Font.Size:=12;
Font.Bold:=0;
Paragraphs.Alignment:=wdAlignParagraphLeft;
TypeText('一、违规SP--北京星天下');
TypeParagraph;
TypeText(richedit1.Text );
TypeParagraph;
TypeParagraph;
TypeText('二、实际拨测步骤 ');
TypeParagraph;
TypeText('1.点开初始链接,进入网站注册页面');
TypeParagraph;
LinkToFile:=False;
SaveWithDocument:=True;
InlineShapes.AddPicture('E:/P3拨测系统/B项目代码/操作WORD/1.jpg',LinkToFile,SaveWithDocument,EmptyParam);
TypeParagraph;
TypeParagraph;
TypeText('2.注册完成后显示');
TypeParagraph;
InlineShapes.AddPicture('E:/P3拨测系统/B项目代码/操作WORD/2.jpg',LinkToFile,SaveWithDocument,EmptyParam);
TypeParagraph;
TypeParagraph;
end;

//保存文档
NoPrompt:=false;
OriginalFormat:=wdOriginalDocumentFormat;
WordApp.Documents.Save(NoPrompt,OriginalFormat);
//关闭文档
SaveChanges:=wdSaveChanges;
OriginalFormat:=wdOriginalDocumentFormat;
RouteDocument:=false;
WordApp.Documents.Close(SaveChanges,OriginalFormat,RouteDocument);

Worddoc.Disconnect ;
worddoc.Free;

//断开和Word 2000的连接
WordApp.Disconnect;
WordApp.Quit ;
WordApp.Free ;
MessageDlg('导出成功!保存为'+fileName,mtInformation,[mbOK],0);

end;


end.

阅读更多

没有更多推荐了,返回首页