unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,word2000,comobj, DB, ADODB, StdCtrls, OleServer;
type
TForm1 = class(TForm)
Button1: TButton;
WordApp: TWordApplication;
Button2: TButton;
Procedure Replace_string(Word_App:TWordApplication;Find_string,Replaced_string:string);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
i:integer;
implementation
{$R *.dfm}
Procedure TForm1.Replace_string(Word_App:TWordApplication;Find_string,Replaced_string:string);
var //查找替换WORD中的指定字符串
FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike,MatchKashida,MatchDiacritics,
MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace,MatchAlefHamza,MatchControl: OleVariant;
Range_start,Range_end,i,Range1:OleVariant;
Document_range:Range;
begin
FindText := Find_string;
MatchCase := False;
MatchWholeWord := True;
MatchWildcards := False;
MatchSoundsLike := False;
MatchAllWordForms := False;
Forward := True;
Wrap := wdFindContinue;
Format := False;
ReplaceWith := Replaced_string;
Replace := wdReplaceAll;
MatchKashida:= False;
MatchDiacritics:= False;
MatchAlefHamza:= False;
MatchControl:= False;
Document_range:=Word_App.ActiveDocument.Range(Range_start,Range_end);
Document_range.Select;
Word_App.Selection.Find.Execute( FindText, MatchCase, MatchWholeWord,
MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
Wrap, Format, ReplaceWith, Replace,MatchKashida,MatchDiacritics,
MatchAlefHamza,MatchControl);
end;
procedure TForm1.Button1Click(Sender: TObject);
var Replace_Range:OleVariant;
FileName,ConfirmConversions,ReadOnly,AddToRecentfiles,PasswordDocument,
PasswordTemplate,Revert,WritePasswordDocument,WritePasswordTemplate,
Format,Encoding,Visible:OleVariant;
begin
WordApp.Connect;
FileName:=GetCurrentDir+'/aaa.doc';
ConfirmConversions:=false;
ReadOnly:=false;
AddToRecentfiles:=false;
PasswordDocument:='';
PasswordTemplate:='';
Revert:=false;
WritePasswordDocument:='';
WritePasswordTemplate:='';
Format:=wdFormatDocument;
//Encoding
Visible:=true;
WordApp.Documents.Open(FileName,ConfirmConversions,ReadOnly,AddToRecentfiles,
PasswordDocument,PasswordTemplate,Revert,WritePasswordDocument,
WritePasswordTemplate,Format,Encoding,Visible);
wordApp.Visible:=true;
//Replace_string(Word_App:TWordApplication;Find_string,Replaced_string:string);
end;
procedure TForm1.Button2Click(Sender: TObject);
var Replace_Range: TWordApplication;
Find_string,ReplacedWith:string;
begin
Replace_Range:=WordApp;
Replace_string(Replace_Range,'<#文档编号#>','ddd');
Replace_string(Replace_Range,'<#文档名称#>','eee');
Find_string:='<#工程类别#>';
ReplacedWith:='aaa';
Replace_string(Replace_Range,find_string,Replacedwith);
Find_string:='<#工程编号#>';
ReplacedWith:='bbb';
Replace_string(Replace_Range,find_string,Replacedwith);
Find_string:='<#工程名称#>';
ReplacedWith:='ccc';
Replace_string(Replace_Range,find_string,Replacedwith);
end;
end.