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
- Get a document with multiple paragraphs.
- Make some parts from the document bold.
- Add debug points in
string searchRangeText = searchRange.Text;andstring foundText = foundRange.Text;. - Run the code to comment the bold parts.
- While you are at it, check the
serachRangeTextandfoundText, and see if thefoundTextis actually from theserachRangeText
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.