Word Range.Find gets stuck in same table cell when searching hidden text and adding comments
23:41 14 Dec 2025

I am writing a function to find hidden text in a Word document and tagging them with comments. I am using Word find to do this. When a hidden text is found inside a table, after the comment is inserted for a hidden text, the range is getting affected and I am now sure how to move on. My current logic gets stuck in the very first hidden text inside the table forever. Below is my code.

   private void RemoveHiddenText()
   {
       if(cleanUpProperties.SelectedcmbHiddenText == 2)
       {
           return;
       }

       Word.Range searchRange = wordDocument.Content.Duplicate; // Ensures full document range
       searchRange.Start = 0;
       searchRange.End = wordDocument.Content.End;

       Word.Find find = searchRange.Find;
       find.ClearFormatting();
       find.Font.Hidden = 1; // True
       find.Text = "";
       find.Forward = true;
       find.Wrap = Word.WdFindWrap.wdFindStop;
       string commentMessage = "";
       Word.Comment comment = null;

       while (find.Execute())
       {      
           switch (cleanUpProperties.SelectedcmbHiddenText)
           {
               case 0:
                   commentMessage = Helper.GetLanguageString("ABC_RemoveHiddenTextsComment") + $"\"{searchRange.Text}\"";
                   comment = searchRange.Comments.Add(searchRange, commentMessage);
                   break;
               case 1:
                   commentMessage = Helper.GetLanguageString("ABC_cmbRemoveHiddenTextsUnhide");
                   comment = searchRange.Comments.Add(searchRange, Helper.GetLanguageString("ABC_cmbRemoveHiddenTextsUnhide"));
                   searchRange.Font.Hidden = 0;
                   break;
               default: break;
           }

           if ((bool)searchRange.Information[Word.WdInformation.wdWithInTable])
           {
               int movingForwardBylength = 1;                   
               searchRange.SetRange(searchRange.End + movingForwardBylength, wordDocument.Content.End);                    
           }
           else
           {
               // Move the search range forward to avoid infinite loop
               searchRange.SetRange(searchRange.End, wordDocument.Content.End);
           }

           // Reconfigure Find for the new range
           find = searchRange.Find;
           find.ClearFormatting();
           find.Font.Hidden = 1;
           find.Text = "";
           find.Forward = true;
           find.Wrap = Word.WdFindWrap.wdFindStop;
       }
   }

Every time when a comment is added, wordDocument.Content.End is increasing by one. But movingForwardBylength is definitely some big number, not 1. but I was unable to find it. Or there may be other logic issue in my code.

Please help me with this.

Thanks in advance.

c# ms-word vsto