如题。比如打开文档1.doc,查找所有“你好”,替换为“你好吗?”
请给出代码。

解决方案 »

  1.   

    you are better refence word vba help article.
      

  2.   

    查过了,不知道怎么做。谁能把代码写出来?不要只说vba三个字母。
      

  3.   

    try
       wApp:=GetActiveOleObject('word.Application');
      except
       wApp:=CreateOleObject('word.Application');
      end;  wApp.selection.find.text:='你好';
      wApp.selection.find.replacement.text:='你好吗';
      wApp.selection.find.execute(replace:=wdReplaceAll);
      

  4.   

    界面上放    WordApplication1, WordDocument1定义一个替换过程
    procedure TForm1.RunReplace(FromStr,ToStr:String);
    var
      FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike,
      MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace: OleVariant;
    begin
      FindText := ToStr;
      MatchCase := False;
      MatchWholeWord := False;
      MatchWildcards := False;
      MatchSoundsLike := False;
      MatchAllWordForms := False;
      Forward := True;
      Wrap := wdFindContinue;
      Format := False;
      ReplaceWith := FromStr;
      Replace := wdReplaceAll;
      WordDocument1.Range.Find.Executeold( FindText, MatchCase, MatchWholeWord,
        MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
        Wrap, Format, ReplaceWith, Replace );
    end;实现替换
    procedure TForm1.Button1Click(Sender: TObject);
    Var
      ItemIndex :OleVariant;  
      FileName, ConfirmConversions, ReadOnly, AddToRecentFiles,
      PasswordDocument, PasswordTemplate, Revert,
      WritePasswordDocument, WritePasswordTemplate, Format: OleVariant;
    begin
      try
      Wordapplication1.Connect;
      except
      Wordapplication1.Quit;
      Application.MessageBox('连接WORD服务器失败,请确定您已经正确安装。','错误框',MB_OK+MB_ICONSTOP);
      Abort;
      end;  FileName:='c:\1.doc';  //参数赋值
      ConfirmConversions := False;
      ReadOnly := False;
      AddToRecentFiles := False;
      PasswordDocument := '';
      PasswordTemplate := '';
      Revert := True;
      WritePasswordDocument := '';
      WritePasswordTemplate := '';
      Format := wdOpenFormatDocument;  //打开文档(模板)
      WordApplication1.Documents.OpenOld( FileName, ConfirmConversions,
        ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate,
        Revert, WritePasswordDocument, WritePasswordTemplate, Format );  ItemIndex := 1;
      WordDocument1.ConnectTo(WordApplication1.Documents.Item(ItemIndex));  {Turn Spell checking off because it takes a long time if enabled and slows down Winword}
      WordApplication1.Options.CheckSpellingAsYouType := False;
      WordApplication1.Options.CheckGrammarAsYouType := False;  RunReplace('你好','你好吗?');  Wordapplication1.ActiveDocument.ActiveWindow.View.type_:=wdPrintView;
      
    end;
      

  5.   

    wApp.Selection.Find.ClearFormatting
        wApp.Selection.Find.Replacement.ClearFormatting
        wApp.Selection.Find
        wApp.Selection.Text = "你好"
        wApp.Selection.Replacement.Text = "你好吗"
        wApp.Selection.Forward = True
        wApp.Selection.Wrap = wdFindContinue
        wApp.Selection.Format = False
        wApp.Selection.MatchCase = False
         wApp.Selection.MatchWholeWord = False
         wApp.Selection.MatchByte = True
         wApp.Selection.MatchWildcards = False
         wApp.Selection.MatchSoundsLike = False
         wApp.Selection.MatchAllWordForms = False
        wApp.Selection.Find.Execute (Replace:=wdReplaceAll)
      

  6.   

    samcrm(镜花水月):你的代码没有把word显示出来啊,而且并没有替换效果。
    我的是word2000
    ghy412(用心良苦):你写完整些行不行?
      

  7.   

    ghy412(用心良苦) 
    samcrm(镜花水月) 二位,受教啦。在下在此谢过!也谢谢楼主开贴。