请问怎样查找在一篇Word文档中指定的一个词一共出现了几次?

解决方案 »

  1.   

    以下是查找与替换的代码,稍加改动即为你所需的:Doc: TWordDocument;
    ....procedure TMain.ReplaceBtnClick(Sender: TObject);
    var
      temp, SearchText, ReplaceText, All: OleVariant;
      findtext, matchcase, matchwholeword, matchwildcards, matchsoundslike,
        matchallwordforms, forward, wrap, format, replacewith, replace,
        matchkashida, matchdiacritics, matchalefhamza, matchcontrol: olevariant;
      TextRange: Range;
      i: byte;
    begin
      TextRange := Doc.Content;
      findtext := 'name';
      replacewith := '张三';
      matchcase := false;
      matchwholeword := true;
      matchwildcards := false;
      matchsoundslike := false;
      matchallwordforms := false;
      forward := true;
      wrap := wdFindContinue;
      format := false;
      Replace := true; 
      i := 0;
      while TextRange.find.execute(findtext, matchcase, matchwholeword,
        matchwildcards, matchsoundslike, matchallwordforms,
        forward, wrap, format, replacewith, replace, matchkashida,
        matchdiacritics, matchalefhamza, matchcontrol)
        do begin
          inc(i);
      end;
    end;
      

  2.   

    我将上面这位弟兄的程序稍稍改动了一下,自己试验了一下,可还是有问题,请大家帮忙看一看。举个例子:假设我要统计一篇文章里出现了多少个“我”我将上面的程序这样修改:Doc: TWordDocument;
    ....procedure TMain.ReplaceBtnClick(Sender: TObject);
    var
      temp, SearchText, ReplaceText, All: OleVariant;
      findtext, matchcase, matchwholeword, matchwildcards, matchsoundslike,
        matchallwordforms, forward, wrap, format, replacewith, replace,
        matchkashida, matchdiacritics, matchalefhamza, matchcontrol: olevariant;
      i: byte;
    begin
    //====================================
      findtext := '我';
      replacewith := '??????';
    //====================================
      matchcase := false;
      matchwholeword := true;
      matchwildcards := false;
      matchsoundslike := false;
      matchallwordforms := false;
      forward := true;
      wrap := wdFindContinue;
      format := false;
      Replace := true; 
      i := 0;
      while Doc.ActiveWindow.Selection.find.execute(findtext, matchcase, matchwholeword,
        matchwildcards, matchsoundslike, matchallwordforms,
        forward, wrap, format, replacewith, replace, matchkashida,
        matchdiacritics, matchalefhamza, matchcontrol)
        do begin
          inc(i);
      end;//===============以下这部分是我加的================
      findtext := '??????';
      replacewith := '我';
      while Doc.ActiveWindow.Selection.find.execute(findtext, matchcase, matchwholeword,
        matchwildcards, matchsoundslike, matchallwordforms,
        forward, wrap, format, replacewith, replace, matchkashida,
        matchdiacritics, matchalefhamza, matchcontrol)
        do begin
          inc(i);
      end;
    //=======================================================
    end;理论上讲,当第一个while执行完后,文章中的“我”全部被替换成“??????”;此后,执行第二个while,又将所有的“??????”,还原成“我”。但实际实行的结果是,当执行到第二个while的时候,始终无法跳出循环,并且,所有的“??????”都没有被还原成“我”。请问我错在哪里?
      

  3.   

    Doc: TWordDocument;
    aCount:integer;
    ....procedure TMain.ReplaceBtnClick(Sender: TObject);
    var
      temp, SearchText, ReplaceText, All: OleVariant;
      findtext, matchcase, matchwholeword, matchwildcards, matchsoundslike,
        matchallwordforms, forward, wrap, format, replacewith, replace,
        matchkashida, matchdiacritics, matchalefhamza, matchcontrol: olevariant;
      TextRange: Range;
      i: byte;
    begin
      TextRange := Doc.Content;
      findtext := 'name';
      replacewith := '张三';
      matchcase := false;
      matchwholeword := true;
      matchwildcards := false;
      matchsoundslike := false;
      matchallwordforms := false;
      forward := true;
      wrap := wdFindContinue;
      format := false;
      Replace := true; 
      i := 0;
      while TextRange.find.execute(findtext, matchcase, matchwholeword,
        matchwildcards, matchsoundslike, matchallwordforms,
        forward, wrap, format, replacewith, replace, matchkashida,
        matchdiacritics, matchalefhamza, matchcontrol)
        do begin
          inc(i);
      end;
      aCount:=i;//这个就是获得出现次数
      Showmessage(IntToStr(aCount));
    end;
      

  4.   

    To luckyboy97(幸运男孩):谢谢你的关注。你回复中说:“ aCount:=i;//这个就是获得出现次数
    ”。这确实没错。但上面的程序执行完后,文章中所有的'name',应经被替换成'张三'。我想把 '张三' 再替换回'name',于是我在后面又加了一段(内容见3楼),这导致了错误。我不明白错在哪里,请指点!