0

I'm using Word Interop Libraries to identify bold text in a Word document and add comments to those sections. However, while running the code, I noticed an issue(from the Word UI): when a bold part or paragraph is encountered, the commenting is inconsistent (Sometimes multiple comments for a single true positive, some are false negatives).

During debugging, I found that some paragraphs are returning false negatives - even though they contain bold parts, the search doesn't recognize them as matches. Following is the implementation details and the specific problem I found while debugging.

Implementation Details

In the current setup (given in the code at the end, and a working sample in GitHub), I'm feeding paragraphs to the search in reverse order (from the end of the document).

string searchRangeText = searchRange.Text;

This line in the sample code correctly shows the paragraph content in every iteration, confirming that all the paragraphs are being fed properly.

I'm taking the Find object from the paragraph's Range object. According to Microsoft's documentation, when find.Execute() runs successfully, it should redefine the original Range object to the range of matching text.

Unexpected Behaviour

After a successful find.Execute() (Where it returns true), I check:

string foundText = foundRange.Text;

But the result is completely different from the original searchRange. In fact, the found text appears to come from previously evaluated ranges, not the current one.

Why is foundRange.Text returning content from earlier paragraphs instead of the current one?
Is there something wrong with how I'm using the Find object?

Any insights or suggestions would be greatly appreciated!

Sample Code - A working example integrated to a sample plugin is available in my GitHub

Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
Word.Paragraph currentPara, nextPara;
currentPara = doc.Paragraphs.Last;

while (currentPara != null)
{
  nextPara = currentPara.Previous();

  Word.Range searchRange = currentPara.Range.Duplicate;
  string searchRangeText = searchRange.Text;

  Word.Find find = searchRange.Find;
  find.ClearFormatting();
  find.Font.Bold = 1;
  find.Wrap = Word.WdFindWrap.wdFindStop;

  while (find.Execute())
  {
    Word.Range foundRange = searchRange.Duplicate;
    string foundText = foundRange.Text;
    foundRange.Comments.Add(foundRange, "Bold Text: '" + foundText + "'");

    if (foundRange.End >= searchRange.End)
    {
      break;
    }
      searchRange.Start = foundRange.End;
  }

  currentPara = nextPara;
}

How to test

  1. Get a document with multiple paragraphs.
  2. Make some parts from the document bold.
  3. Add debug points in string searchRangeText = searchRange.Text; and string foundText = foundRange.Text;.
  4. Run the code to comment the bold parts.
  5. While you are at it, check the serachRangeText and foundText, and see if the foundText is actually from the serachRangeText

Edit: After @jonsson 's comment I realized that the way I have presented the question previously was a bit misleading. Specially about what problem I'm trying to solve here. So, I'm editing the original question by making it little more generic.

5
  • 1
    I think you have envcountered the problem where you can find things in the defined range, but cannot find the defined range's text (see, e.g. gregmaxey.com/word_tip_pages/… ). If all you need to do is search for paragraphs that are completely bold, it might be simpler to iterate the paragraphs and check that the paragraph.range.text is bold. Commented Sep 11 at 9:18
  • Actually the unexpected behaviour (right paragraph, but wrong results) occurs every time. I tried to simplify the question with the specific situation involving whole paragraphs. But looks like that made my question a bit misleading. I'll edit the question, and Thank you for your suggestion and the resource. I'll read that and come back. Commented Sep 12 at 5:36
  • 1
    TBH I still think the problem is broadly speaking the same as the one described by Greg Maxey. Suppose you have a Find Range containing "abc" , you want to find "abc", and you provide a replacement text "xyz". What happens depend on whether there is a later "abc" after your original Find Range. If there is, the .Execute returns True, the later abc changes to xyz, which is the new FInd Range (i.e. outside the original FInd Range). If there is not, the .Execute still returns True, but no text changes and the original FInd Range does not change.... Commented Sep 13 at 18:39
  • 1
    ...That matches the case where your original FInd Range is a complete Bold paragraph, you are looking for "Bold" (but with no text specified) and there is Bold text after the original FInd Range paragraph. It may be that one of Greg's article provides a generic answer, or that in your case, it is enough to collapse your Find Range to the beginning of the paragraph before executing the Find, but I think you always have to verify that an approach like that works for the specific FInd/Replace you ar attempting. Commented Sep 13 at 18:43
  • 1
    Why are you looping by paragraph instead of simply doing a Find for bold ranges and looping via them? See, for example: msofficeforums.com/61506-post3.html Commented Sep 14 at 3:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.