如何在一整篇文档中查找并替换所有相同的文字(100分)

  • 主题发起人 主题发起人 tly126
  • 开始时间 开始时间
T

tly126

Unregistered / Unconfirmed
GUEST, unregistred user!
Sub Macro11()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "yxx"
.Replacement.Text = "bb"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
上面的宏如何用delphi实现主要是这句‘Selection.Find.Execute Replace:=wdReplaceAll‘
 
打开DELPHI
转到severs组件板
拉一个WordApplication1,改名为WordApplication
一个WordDocument1改名为WordDocument
转到dialogs组件板
拉一个OpenDialog1,改名为dlgopen
再拉4个button,将以下四段代码依次放入就行了
启动word时用如下代码:
begin
try
wordapplication.connect;
except
messagedlg('word may not be installed', mterror, [mbok], 0);
abort;
end;
wordapplication.visible := true;
wordapplication.caption := 'delphi automation';
end;
/////////////////////////////////////
关闭word用如下代码。如果想保存doc文件,请修改savechanges变量的内容:
var
savechanges, originalformat, routedocument: olevariant;
begin
savechanges := wddonotsavechanges;
originalformat := unassigned;
routedocument := unassigned;
try
wordapplication.quit(savechanges, originalformat, routedocument);
wordapplication.disconnect;
except
on e: exception do
begin
showmessage(e.message);
wordapplication.disconnect;
end;
end;
end;
/////////////////////////////////////
让word打开一个指定的文件,需要先放置opendialog,然后调用wordapplication.documents.open:
var
itemindex :olevariant;
filename, confirmconversions, readonly, addtorecentfiles,
passworddocument, passwordtemplate, revert,
writepassworddocument, writepasswordtemplate, format: olevariant;
begin
if not dlgopen.execute then
exit;
{open document}
filename := dlgopen.filename;
confirmconversions := false;
readonly := false;
addtorecentfiles := false;
passworddocument := '';
passwordtemplate := '';
revert := true;
writepassworddocument := '';
writepasswordtemplate := '';
format := wdopenformatdocument;
wordapplication.documents.open( filename, confirmconversions,
readonly, addtorecentfiles, passworddocument, passwordtemplate,
revert, writepassworddocument, writepasswordtemplate, format );
{assign worddocument component}
itemindex := 1;
worddocument.connectto(wordapplication.documents.item(itemindex));
{turn spell checking of because it takes a long time if enabled and slows down winword}
wordapplication.options.checkspellingasyoutype := false;
wordapplication.options.checkgrammarasyoutype := false;
end;
/////////////////////////////////////
让word替换标记字符串要使用worddocument.range.find.execute,这里用delphi替换了<#name>:
var
findtext, matchcase, matchwholeword, matchwildcards, matchsoundslike,
matchallwordforms, forward, wrap, format, replacewith, replace: olevariant;
begin
findtext := '<#name>';
matchcase := false;
matchwholeword := true;
matchwildcards := false;
matchsoundslike := false;
matchallwordforms := false;
forward := true;
wrap := wdfindcontinue;
format := false;
replacewith := 'delphi';
replace := true;
worddocument.range.find.execute( findtext, matchcase, matchwholeword,
matchwildcards, matchsoundslike, matchallwordforms, forward,
wrap, format, replacewith, replace );
end;
/////////////////////////////////////


2004-5-2 15:28:54 function rep(a:string;b:String):boolean;
var
findtext, matchcase, matchwholeword, matchwildcards, matchsoundslike,
matchallwordforms, forward, wrap, format, replacewith, replace:

olevariant;
begin
findtext := a;
matchcase := false;
matchwholeword := true;
matchwildcards := false;
matchsoundslike := false;
matchallwordforms := false;
forward := true;
wrap := wdfindcontinue;
format := false;
replacewith := b;
replace := true;
form1.worddocument.range.find.execute( findtext, matchcase, matchwholeword,
matchwildcards, matchsoundslike, matchallwordforms, forward,
wrap, format, replacewith, replace );
end;

 
多谢关照!
我没把以前的问题说太清楚,下面我具体点:
我编的程序是打印学生的转学证明,学生的姓名,学校]等信息从数据库调入,替换我先前定义好的模版上的文字。我以前也是按你您的方法作的,可是只能替换一行文字(如下所示我要把yxx替换为nnnnn):
替换前(
[blue]yxx:
学生xm性别xb年龄nl学籍号yxjh系我
县zzh人,现在你校nj年级学习,因yy
申请转入n,经县局审核,符合学籍管
理规定,同意转学。

年月日



xxx:
学生xm性别xb年龄nl学籍号xxjh系我
县zzh人,现在yxx nj年级学习,因yy
申请转入你校,经县局审核,符合学籍管
理规定,同意转学。

年月日[/blue])

替换后

[red]nnnnn:
学生xm性别xb年龄nl学籍号yxjh系我
县zzh人,现在你校nj年级学习,因yy
申请转入n,经县局审核,符合学籍管
理规定,同意转学。

年月日



xxx:
学生xm性别xb年龄nl学籍号xxjh系我
县zzh人,现在yxx nj年级学习,因yy
申请转入你校,经县局审核,符合学籍管
理规定,同意转学。

年月日[/red])
上面的文字中的'yxx'我如何都替换为‘nnnnn’
 
function rep(a:string;b:String):boolean;
var
findtext, matchcase, matchwholeword, matchwildcards, matchsoundslike,
matchallwordforms, forward, wrap, format, replacewith, replace:

olevariant;
begin
findtext := a;
matchcase := false;
matchwholeword := true;
matchwildcards := false;
matchsoundslike := false;
matchallwordforms := false;
forward := true;
wrap := wdfindcontinue;
format := false;
replacewith := b;
replace := true;
form1.worddocument.range.find.execute( findtext, matchcase, matchwholeword,
matchwildcards, matchsoundslike, matchallwordforms, forward,
wrap, format, replacewith, replace );
end;

 
后退
顶部