简体   繁体   English

如何在背景不同的Word文件中查找文本?

[英]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. 我需要在Word文件中搜索并找到具有不同背景颜色的所有文本(突出显示),然后仅将已建立的文本写入新的Word文件中。 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 ? 是否有所有Office OLE对象方法的文档?

The code below shows how to scan a Word document character-by-character and reports the background color of the character: 下面的代码显示如何逐字符扫描Word文档并报告字符的背景色:

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. 该代码正确地将HighlightColorIndex的中间部分报告为3,其余部分报告为0。

For reference, see eg 供参考,例如

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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