简体   繁体   中英

How to find text inside word file that has different background?

I need to search inside Word file and find all the text that has different background colors (highlighted) and write only the founded text into new word file. how I can do that ?

I tried using this code:

uses ActiveX, ComObj;
const
  wdFindContinue = 1;
  wdLine=5;
  wdStory = 6;
  wdExtend = 1;
  wdCharacter = 1;
var
  OleObj: Variant;
begin
  // create OLE object for MS Word application:
  OleObj := CreateOLEObject('Word.Application');
  // load a document from your file
  OleObj.Documents.Open(YourDocument);
  OleObj.Selection.Find.ClearFormatting;
  OleObj.Selection.Find.Text := strFinna;
  // WordApp.Selection.Find.Replacement.Text := yourNewStringForReplace;
  OleObj.Selection.Find.Forward := True;
  OleObj.Selection.Find.MatchAllWordForms := False;
  OleObj.Selection.Find.MatchCase := False;
  OleObj.Selection.Find.MatchWildcards := False;
  OleObj.Selection.Find.MatchSoundsLike := False;
  OleObj.Selection.Find.MatchWholeWord := False;
  OleObj.Selection.Find.MatchFuzzy := False;
  OleObj.Selection.Find.Wrap := wdFindContinue;
  OleObj.Selection.Find.Format := False;
  OleObj.Selection.HomeKey(unit := wdStory);
  while OleObj.Selection.Find.Execute do
  begin
    OleObj.Selection.EndKey(Unit := wdLine, Extend := wdExtend);
    OleObj.Selection.MoveRight(Unit := wdCharacter, Count := 1);
    OleObj.Selection.MoveUp(Unit := wdLine, Count := 1, Extend := wdExtend);
    OleObj.Selection.Delete(Unit := wdCharacter, Count := 1);
  end;
  OleObj.ActiveDocument.Save;
  OleObj.Quit;
  OleObj := Unassigned;
end;

Is there any documentation for all office OLE Object methods ?

The code below shows how to scan a Word document character-by-character and reports the background color of the character:

procedure TForm1.DoCheckBackground;
var
  OleObj: Variant;
  YourDocument : String;
  Moved : Integer;
  Range : OleVariant;
  Color : TColor;
begin

  YourDocument := 'D:\aaad7\officeauto\parabackground.docx';
  // create OLE object for MS Word application:
  OleObj := CreateOLEObject('Word.Application');
  OleObj.Visible := True;
  // load a document from your file
  OleObj.Documents.Open(YourDocument);
  repeat
    Moved := OleObj.Selection.MoveRight(Unit := wdCharacter, Count := 1);
    if Moved > 0 then begin
      Range := OleObj.Selection.Range;
      Color := Range.HighlightColorIndex; 
      Memo1.Lines.Add(OleObj.Selection.Text + ':' + IntToStr(Color));
    end;
  until Moved <= 0;

I tested this with a document containing a middle paragraph which has a turquoise background, the rest being white. The code correctly reports the HighlightColorIndex as 3 for the middle para and 0 for the rest.

For reference, see eg

https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.range.highlightcolorindex.aspx

https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.selection.moveright.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM