try{
    wordApplication = CreateOleObject("Word.Application");
    wordApplication.OlePropertySet("Visible", 1);
  }
  catch(...)
  {
    ShowMessage("您可能没有安装Word!");
    return;
  }
    documents = wordApplication.OlePropertyGet("Documents");
    document = documents.OleFunction("Open", "c:/12.doc");
    document.OleProcedure("Activate");
 selection = wordApplication.OlePropertyGet("Selection");
 selection.OleProcedure("WholeStory");
  Variant oFind = selection.OlePropertyGet("Find");
  oFind.OleProcedure("ClearFormatting");
  Variant oReplace = oFind.OlePropertyGet("Replacement");
  oReplace.OleProcedure("ClearFormatting");
  oFind.OlePropertySet("Text", "<BBH>");
  oReplace.OlePropertySet("Text", "编号");
  oFind.OlePropertySet("Forward", true);
  oFind.OlePropertySet("Wrap",wdFindContinue);
  oFind.OlePropertySet("Format", false);
  oFind.OlePropertySet("MatchCase", false);
  oFind.OlePropertySet("MatchWholeWord", false);
  oFind.OlePropertySet("MatchByte", true);
  oFind.OlePropertySet("MatchWildcards", false);
  oFind.OlePropertySet("MatchSoundsLike", false);
  oFind.OlePropertySet("MatchAllWordForms", false);
  oFind.OleProcedure("Execute","Replace=wdReplaceAll");
  wordApplication.OleFunction("Quit");
为何替换不了呀?

解决方案 »

  1.   

    不知道楼主的问题有解决没有,接一份代码先
    ----------------------------------------
    {
      This function starts a hidden word instance, opens a specified document "ADocument"
      and replaces (all) occurrences of "SearchString" with a specified "ReplaceString".
      The function is similar to the Delphi StringReplace() function.
    }{
      Diese Funktion startet eine Word versteckte Instanz, &ouml;ffnet eine Datei und
      ersetzt (alle) Vorkommen von "SearchString" durch "ReplaceString".
      Die Funktion ist &auml;hnlich zur StringReplace() Funktion.
    }
    uses
      ComObj;// Replace Flags
    type
      TWordReplaceFlags = set of (wrfReplaceAll, wrfMatchCase, wrfMatchWildcards);function Word_StringReplace(ADocument: TFileName; SearchString, ReplaceString: string; Flags: TWordReplaceFlags): Boolean;
    const
      wdFindContinue = 1;
      wdReplaceOne = 1;
      wdReplaceAll = 2;
      wdDoNotSaveChanges = 0;
    var
      WordApp: OLEVariant;
    begin
      Result := False;  { Check if file exists }
      if not FileExists(ADocument) then
      begin
        ShowMessage('Specified Document not found.');
        Exit;
      end;  { Create the OLE Object }
      try
        WordApp := CreateOLEObject('Word.Application');
      except
        on E: Exception do
        begin
          E.Message := 'Word is not available.';
          raise;
        end;
      end;  try
        { Hide Word }
        WordApp.Visible := False;
        { Open the document }
        WordApp.Documents.Open(ADocument);
        { Initialize parameters}
        WordApp.Selection.Find.ClearFormatting;
        WordApp.Selection.Find.Text := SearchString;
        WordApp.Selection.Find.Replacement.Text := ReplaceString;
        WordApp.Selection.Find.Forward := True;
        WordApp.Selection.Find.Wrap := wdFindContinue;
        WordApp.Selection.Find.Format := False;
        WordApp.Selection.Find.MatchCase := wrfMatchCase in Flags;
        WordApp.Selection.Find.MatchWholeWord := False;
        WordApp.Selection.Find.MatchWildcards := wrfMatchWildcards in Flags;
        WordApp.Selection.Find.MatchSoundsLike := False;
        WordApp.Selection.Find.MatchAllWordForms := False;
        { Perform the search}
        if wrfReplaceAll in Flags then
          WordApp.Selection.Find.Execute(Replace := wdReplaceAll)
        else
          WordApp.Selection.Find.Execute(Replace := wdReplaceOne);
        { Save word }
        WordApp.ActiveDocument.SaveAs(ADocument);
        { Assume that successful }
        Result := True;
        { Close the document }
        WordApp.ActiveDocument.Close(wdDoNotSaveChanges);
      finally
        { Quit Word }
        WordApp.Quit;
        WordApp := Unassigned;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      Word_StringReplace('C:\Test.doc','Old String','New String',[wrfReplaceAll]);
    end;
      

  2.   

    这东西我感觉最好的方法是在word里做相同的操作,然后用宏记录,再看宏的代码,翻译成Delphi的语法,基本都OK。