有关WORD的问题(20分)

  • 主题发起人 主题发起人 mylemontree
  • 开始时间 开始时间
M

mylemontree

Unregistered / Unconfirmed
GUEST, unregistred user!
我在原来的帖子中找到,这样一段代码:

/replace a specified string
procedure TForm1.Button4Click(Sender: TObject);
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 := wdFindStop;
Format := False;
ReplaceWith := 'Delphi';//替换后的字符串
Replace := wdReplaceAll;

WordDocument1.Range.Find.Execute(FindText, MatchCase, MatchWholeWord,
MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
Wrap, Format, ReplaceWith, Replace);
end;

但在我的程序中实际RUN时,没有替换特定的字符串,而是新建了一个WORD文档,WHY
 
用用下面这段代码(Delphi6下用),我已经试过没有问题
procedure TMainForm.Button2Click(Sender: TObject);
var
findtext, matchcase, matchwholeword, matchwildcards, matchsoundslike,
matchallwordforms, forward, wrap, format, replacewith, replace,
matchkashida,matchdiacritics,matchalefhamza,matchcontrol: olevariant;
begin
findtext := '123';
matchcase := false;
matchwholeword := true;
matchwildcards := false;
matchsoundslike := false;
matchallwordforms := false;
forward := true;
wrap := wdfindcontinue;
format := false;
replacewith := 'ABC';
replace := true;
worddocument.range.find.execute( findtext, matchcase, matchwholeword,
matchwildcards, matchsoundslike, matchallwordforms,
forward,wrap, format, replacewith, replace,matchkashida,matchdiacritics,matchalefhamza,matchcontrol );
end;
 
to kuangwenwei
我是在DELPHI6下用的,代码停在最后一行的matchkashida处,提示
[Error] Unit1.pas(114): Too many actual parameters
 
因為你沒有讓程式得到擊活的 MSWord
你用這個方法 WordDoc.ActiveWindow.Document.Range.Find.Execute
 
to alextsui
说具体点好吗?我要在什么时候激活它,你告诉我的代码应加在什么地方。
 
可以这样试试:
var
FindText,WordObject, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike,
MatchAllWordForms, Forward1, Wrap, Format, ReplaceWith, Replace: OleVariant;
begin
FindText := '[JBWH]';//被替换字符
MatchCase := False;
MatchWholeWord := True;
MatchWildcards := False;
MatchSoundsLike := False;
MatchAllWordForms := False;
Forward1 := True;
Wrap := wdFindContinue;
Format := False;
ReplaceWith := '举交字第001号';//替换字符
Replace := True;
WordObject:=CreateOLEObject('Word.Application');
WordObject.Visible := True;

try
WordObject.Documents.open('e:/JBH.doc',true);
WordObject.Application.ActiveDocument.Range.Find.Execute( FindText, MatchCase, MatchWholeWord,
MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward1,
Wrap, Format, ReplaceWith, Replace );
finally
WordObject.Application.ActiveDocument.SaveAs('c:/my documents/aa',wdFormatDocument);
//WordObject.Application.Windows('jbh.doc').Activate;
//WordObject.Application.ActiveWindow.close;



end;
 
如果要被替换的字符串是写在Word的文本框里以上的程序段可以正常工作吗?
 
后退
顶部