关于word查找替换功能的delphi调用(50分)

  • 主题发起人 主题发起人 Blue_Fire
  • 开始时间 开始时间
B

Blue_Fire

Unregistered / Unconfirmed
GUEST, unregistred user!
下面的代码1是word97宏生成的,目的是将文档中的huang全部替换为wang,我现在想在delphi里实现这个功能,
代码2是经过转换后的,但是看不到运行效果(能够查找,但是没法替换),请教!
代码1
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "huang"
.Replacement.Text = "wang"
.Forward = True
.Wrap = 1
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.MatchByte = True
End With
Selection.Find.Execute Replace:=2


代码2
uses ComObj
var
word_handle:oleviarant;

procedure form1.button1click(sender:Tobject);
begin
word_handle.Selection.Find.ClearFormatting; //清空待查字符串输入框
word_handle.Selection.Find.Replacement.ClearFormatting; //清空替换字符串输入框
word_handle.selection.find.Text = "huang"; //设置待查字符串
word_handle.selection.find.Replacement.Text = "wang"; //设置替换字符串
//其他查找属性
word_handle.selection.find.Forward = True;
word_handle.selection.find.Wrap = 1;
word_handle.selection.find.Format = False;
word_handle.selection.find.MatchCase = False;
word_handle.selection.find.MatchWholeWord = False;
word_handle.selection.find.MatchWildcards = False;
word_handle.selection.find.MatchSoundsLike = False;
word_handle.selection.find.MatchAllWordForms = False;
word_handle.selection.find.MatchByte = True;
//执行全部替换动作
word_handle.selection.find.Execute(2);
end;

 
下面是我写的一个类,可以打开,替换word中的字符
unit wordcls;

interface
uses
Windows, Classes, ActiveX, Word97,registry;

type
TWordobj =class
private
FWordApp : _Application;
public
constructor Create;
destructor Destroy; override;
procedure CloseDoc;
procedure find(str1,str2 : String);
procedure SAVE();
procedure open(file_name : String);
function test_word(sql_1:string):Boolean;
published
property Application : _Application read FWordApp;
end;
implementation



constructor TWordObj.Create;
begin
FWordApp := CoWordApplication.Create;
end;

destructor TWordObj.Destroy;
var
SaveChanges,
OriginalFormat,
RouteDocument : OleVariant;
begin
SaveChanges := WdDoNotSaveChanges;
OriginalFormat := UnAssigned;
RouteDocument := UnAssigned;
try
FWordApp.Quit(SaveChanges,OriginalFormat,RouteDocument);
except
end;
inherited Destroy;
end;

procedure TWordObj.CloseDoc;
var
SaveChanges,
OriginalFormat,
RouteDocument : OleVariant;
begin
SaveChanges := WdDoNotSaveChanges;
OriginalFormat := UnAssigned;
RouteDocument := UnAssigned;
FWordApp.ActiveDocument.Close(SaveChanges,OriginalFormat,RouteDocument);
end;
procedure TWordObj.SAVE();
VAR
NoPrompt,OriginalFormat: OleVariant;
BEGIN
NoPrompt:=TRUE;
OriginalFormat:= UnAssigned;
FWordApp.Documents.Save(NoPrompt,OriginalFormat);
END;
procedure TWordObj.find(str1,str2 : String);
var
findtext, matchcase, matchwholeword, matchwildcards, matchsoundslike,
matchallwordforms, forward, wrap, format, replacewith, replace: OleVariant;
begin
findtext :=str1;
matchcase := false;
matchwholeword := true;
matchwildcards := false;
matchsoundslike := false;
matchallwordforms := false;
forward := true;
wrap := wdFindContinue;
format := false;
replacewith :=str2;
replace := true;
FWordApp.Selection.Range.Find.Execute(findtext, matchcase, matchwholeword,
matchwildcards, matchsoundslike, matchallwordforms, forward,
wrap, format, replacewith, replace );
end;

procedure TWordObj.open(file_name : String);
var
FileName,ConfirmConversions,ReadOnly,AddToRecentFiles,
PasswordDocument,PasswordTemplate,Revert,WritePasswordDocument,
WritePasswordTemplate,Format:OleVariant;
begin
FileName:=file_name;
ConfirmConversions:=false;
ReadOnly:=false;
AddToRecentFiles:=false;
PasswordDocument:='';
PasswordTemplate:='';
Revert:=false;
WritePasswordDocument:='';
WritePasswordTemplate:='';
Format:=wdOpenFormatAuto;
FWordApp.documents.Open(FileName,ConfirmConversions,
ReadOnly,AddToRecentFiles,PasswordDocument,
PasswordTemplate,Revert,WritePasswordDocument,
WritePasswordTemplate,Format);
FWordApp.Visible:=true;
end;

function Twordobj.test_word(sql_1:string):Boolean;
var
path:string;
reg:TRegistry;
begin
Reg := TRegistry.Create;
reg.RootKey:=HKEY_LOCAL_MACHINE;
path:='/SOFTWARE/Microsoft/Office/Word';
If Reg.OpenKey(path, False) then
result:=true
else
result:=false;
end ;

end.
 
Word的替换应该不难,你的代码中好像Find对象的Execute方法用的不对,
以下是我的函数:

function TsqWDBase.ReplaceText(pFindText,pReplaceText: string): Boolean;
var
lFindText,lMatchCase,lMatchWholeWord,lMatchWildcards,
lMatchSoundsLike,lMatchAllWordForms,
lForward,lWrap,lFormat,lReplaceWith,lReplace,
lMatchKashida,lMatchDiacritics,
lMatchAlefHamza,lMatchControl: OleVariant;
begin
Result:= False;
lFindText:= pFindText;
lMatchCase:= False;
//全字匹配
lMatchWholeWord:= False;
lMatchWildcards:= False;
lMatchSoundsLike:= False;
lMatchAllWordForms:= False;
lForward:= True;
lWrap:= wdFindStop;
lFormat:= False;
lReplaceWith:= pReplaceText;
//全部替换还是只替换第一个
lReplace:= wdReplaceOne;
lMatchKashida:= False;
lMatchDiacritics:= False;
lMatchAlefHamza:= False;
lMatchControl:= False;
Result:= FWordDoc.Range.Find.Execute(lFindText,lMatchCase
,lMatchWholeWord,lMatchWildcards,
lMatchSoundsLike,lMatchAllWordForms,
lForward,lWrap,lFormat,lReplaceWith,lReplace,
lMatchKashida,lMatchDiacritics,
lMatchAlefHamza,lMatchControl);
end;//end of proc
 
我用Delphi的server里面的组件实现的,下面这个函数是可以用的,参考一下吧。

procedure RepData(MyDoc: TWordDocument; FindStr, RepStr: string); //利用数据库替换Word字符串函数
//FindStr:被查找的字符串。RepStr:要替换的字符串。
var
MatchCase, MatchWholeWord, find1, rep1, Format,
MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
Wrap, Replace: OleVariant;
{以上函数为Word实现替换的系统变量,详情请参阅Word的在线帮助。}
begin
Find1 := FindStr; //替换的目的字符串。
Rep1 := RepStr; //替换的源字符串。
MatchCase := false;
MatchWholeWord := true;
MatchWildcards := false;
MatchSoundsLike := false;
MatchAllWordForms := false;
Format := true;
Forward := true;
replace := wdReplaceall;
{if} Mydoc.Range.Find.Execute(find1, MatchCase, MatchWholeWord,
MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
Wrap, Format, Rep1, Replace);
end;
//利用数据库替换Word字符串函数(结束)
 
谢谢Freebit,ffsquell,天与地。
从你们的帮助中我找到了答案,再次谢谢!
还有一个疑惑,你们的代码中的WdDoNotSaveChanges,WdDoNotSaveChanges,
wdFindContinue,wdReplaceOne,wdReplaceall等等常量是不是因为装载了word97单元
所以才能使用的?


 
wdReplaceOne,wdReplaceAll...是常数。
导入Word类型库,或引用(uses)Delphi中已有Word的单元,都可以看到这些常量。
 
Lots Thanks to ffsquell!
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
I
回复
0
查看
446
import
I
I
回复
0
查看
697
import
I
后退
顶部