导出文字和图片到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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值