您现在的位置:首页 >> API >> API >> 内容

Delphi操作Word改变指定段落格式

时间:2011/9/3 15:44:59 点击:

  核心提示:WordXP.pas单元在C:\Program Files\Borland\Delphi7\Ocx\Serversunit Unit1;interfaceuses Windows, Messages,...

WordXP.pas单元在C:\Program Files\Borland\Delphi7\Ocx\Servers

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, comobj, WordXP, StdCtrls, StrUtils,  jpeg,
  ExtCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Button2: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    Image1: TImage;
    OpenDialog1: TOpenDialog;
    UpDown1: TUpDown;
    Edit2: TEdit;
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  //把这两个变量声明为全局变量
  Ole: Variant;
  NewDoc: Variant;
  Paragra: Variant;

implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
var
  i: Integer;
  FileName: string;
begin
  if OpenDialog1.Execute and (OpenDialog1.FileName <> '') then
    FileName := OpenDialog1.FileName
  else
    begin
      MessageBox(Handle, '请选择要处理的文件', '提示', MB_OK + MB_ICONINFORMATION);
      Exit;
      end;

 try
  Ole := CreateOleObject('Word.Application');
  Ole.Application.Visible := true;
  NewDoc := Ole.Documents.open(FileName);

  for i := 1 to NewDoc.Paragraphs.count do
  begin
    Paragra := NewDoc.Paragraphs.item(i);
    //LeftStr(Paragra.range.text,1);
    //Memo1.Lines.Add(LeftStr(trim(Paragra.range.text), 1));
    if LeftStr(trim(Paragra.range.text), 1) = '▲' then
    begin
      Paragra.range.Font.Name := trim(Edit1.Text);
      Paragra.range.Font.size := StrToInt(Edit2.Text);
      Paragra.range.Font.Bold := CheckBox1.Checked;
      Paragra.range.Font.Italic := CheckBox2.Checked;
    end;
  end;

  //设置Variant对象为UnAssigned来释放该对象的引用。
  NewDoc := UnAssigned;
  Ole := UnAssigned;
  finally
        MessageBox(Handle, '文件处理完毕', '提示', MB_OK + MB_ICONINFORMATION);
    end;
end;

end.

作者:网络 来源:www.wesoho
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
本类推荐
  • 没有
本类固顶
  • 没有
  • 盒子文章(www.2ccc.com) © 2022 版权所有 All Rights Reserved.
  • 沪ICP备05001939号