create Findnext function for Word document with C#
I am working on a Word add-in.
I want to create Find and FindNext function.
I created a search condition to find a string in a document.
It works. But I want to jump forward or a previously found item and click on Replace button each I want.
now when I click on FindNext when I don't click on Replace the FindNext does not work.
I think it is related to the index. how can I create an index in the document?
[url]https://stackoverflow.com/questions/67384094/create-findnext-function-for-word-document-with-c-sharp[/url]
Microsoft.Office.Interop.Word.Document dc = Globals.ThisAddIn.Application.ActiveDocument;
Microsoft.Office.Interop.Word.Range rng = dc.Content;
dc.Activate();
rng.Find.ClearFormatting();
rng.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
rng.Find.Format = false;
rng.Find.MatchCase = false;
rng.Find.MatchWholeWord = false;
rng.Find.MatchWildcards = false;
rng.Find.MatchSoundsLike = false;
rng.Find.MatchAllWordForms = false;
rng.Find.Forward = true;
object missing = System.Reflection.Missing.Value;
object repl = Microsoft.Office.Interop.Word.WdReplace.wdReplaceNone;
object Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
if (rng.Find.Execute(" ", missing, missing, missing, missing, missing, missing, Wrap, missing, "", repl, missing, missing, missing, missing))
{
var numPages = Convert.ToString(rng.get_Information(WdInformation.wdActiveEndPageNumber));
MessageBox.Show(String.Format("Page No :{0}", numPages), "Double Spaces issue");
rng.Find.HitHighlight(FindText: " ", MatchCase: true, HighlightColor: WdColor.wdColorYellow);
}
else
{
MessageBox.Show("Issues not found.", "Double Spaces issue");
}