VBA中execute的写法: 
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "124"
        .Replacement.Text = "421"
        .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         <------------
<
在delphi中要怎么表达,我试了
    worddoc.content.Find.ClearFormatting;
    worddoc.content.Find.Replacement.ClearFormatting;
        worddoc.content.Find.Text := '124';
        worddoc.content.Find.Replacement.Text := '421';
        worddoc.content.Find.Forward := True;
        worddoc.content.Find.Format := False;
        worddoc.content.Find.MatchCase := False;
        worddoc.content.Find.MatchWholeWord := False;
        worddoc.content.Find.MatchByte := True;
        worddoc.content.Find.MatchWildcards := False;
        worddoc.content.Find.MatchSoundsLike := False;
        worddoc.content.Find.MatchAllWordForms := False;
    worddoc.content.Find.Execute('Wrap=wdFindContinue;Replace=wdReplaceAll'); <-----------
不起作用,
请问 execute要怎么写.
或者告诉我怎样控制对word文档中内容的查找替换

解决方案 »

  1.   

    uses ComObj;var
      WordApp,WordDoc:OleVariant;  procedure TForm1.Button1Click(Sender: TObject);
    begin
      WordApp:=CreateOleObject('Word.Application');        //启动Word
      WordDoc:=WordApp.Documents.Open('E:\Test.doc');      //打开测试文档
      //  下一句决定是否显示Word
      //  WordApp.Visible:=True;
      WordApp.Selection.Find.Execute('124', , , , , , , , , '421', 2, , , , );  //替换
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
        WordDoc.Save;                //保存
        WordApp.Quit;                //退出
        WordApp:=Unassigned;
        WordDoc:=Unassigned;
    end;
      

  2.   

    Execute那一句中的2表示全部替换
      

  3.   

    var
        wordApp : Variant;
        wdReplaceAll;
    begin
        try
            wordApp := CreateOleObject('Word.Application');
            wordApp.visible := true;
        except
            MessageBox(0,'请确定是否安装了Word',MB_OK+MB_ICONINFORMATION);
            Exit;
        end;
        wordApp.Documents.Open('c:\test.doc',False,False,False,'','',false);
        wordApp.Selection.Find.ClearFormatting;
        wordApp.Selection.Find.Replacement.ClearFormatting;
        wordApp.Selection.Find.Text := '1234';
        wordApp.Selection.Find.Replacement.Text :='4321';
        wordApp.Selection.Find.Forward :=True;
        wdFindContinue := 1;
        wordApp.Selection.Find.Wrap := wdFindContinue;
        wordApp.Selection.Find.Format :=False;
        wordApp.Selection.Find.MatchCase :=False;
        wordApp.Selection.Find.MatchWholeWord :=False;
        wordApp.Selection.Find.MatchByte :=True;
        wordApp.Selection.Find.MatchWildcards :=False;
        wordApp.Selection.Find.MatchSoundsLike :=False;
        wordApp.Selection.Find.MatchAllWordForms :=False;
        wdReplaceAll := 2;    //下面这句最重要,是将文档中所有为1234的字符替换成4231,在Delphi中调用Word的宏命令
        //如果有缺省参数可以不写后面直接打逗号
        wordApp.Selection.Find.Execute('1234',,,,,,,wdFindContinue,,'4231',wdReplaceAll);
    end;
      

  4.   

    其实不难,我试了一下,下面的代码可以实现全部替换
    var
      fWord: variant;
    begin
      fWord := createoleobject('word.application');
      fword.Documents.Open('D:\ttt.doc');
      fword.visible := true;
       fword.Selection.Find.ClearFormatting;
        fword.Selection.Find.Replacement.ClearFormatting;
      fword.Selection.Find.Text:='查找字符串';
            fword.Selection.Find.Replacement.Text := '替换字符串';
            fword.Selection.Find.Forward := True;
            fword.Selection.Find.Wrap := 1;
            fword.Selection.Find.Format := False;
            fword.Selection.Find.MatchCase := False;
            fword.Selection.Find.MatchWholeWord := False;
            fword.Selection.Find.MatchByte := True;
            fword.Selection.Find.MatchWildcards := False;
            fword.Selection.Find.MatchSoundsLike := False;
            fword.Selection.Find.MatchAllWordForms := False;    fword.Selection.Find.Execute(Replace:=2);    fword.Selection.Find.Execute;
        fword.activedocument.save;
        fword.quit;
        fword := unassigned;利用createoleobject的好处是不用了解word具体的操作命令
    录制的vba代码可以照搬过来,要注意的无非是将vba定义的常量转换为数字
    如:wdReplaceAll转换成2
    具体常量对应的数值可以在vba中跟踪到