create Findnext function for Word document with C#

Sh Ghiassi 21 Reputation points
2021-05-04T10:26:57.907+00:00

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");
        }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,289 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,517 questions
0 comments No comments
{count} votes