none
C#: Searching a Text in Word an getting the range of the result RRS feed

  • Question

  • Hi,

    I can find text with the code:

    dc.Activate();
    dc.Content.Find.ClearFormatting();
    dc.Content.Find.Wrap = WdFindWrap.wdFindContinue;
    dc.Content.Find.Format = false;
    dc.Content.Find.MatchCase = false;
    dc.Content.Find.MatchWholeWord = false;
    dc.Content.Find.MatchWildcards = false;
    dc.Content.Find.MatchSoundsLike = false;
    dc.Content.Find.MatchAllWordForms = false;
    dc.Content.Find.Forward = true;
    object missing = System.Reflection.Missing.Value;
    object repl = WdReplace.wdReplaceNone;
    object Wrap = WdFindWrap.wdFindContinue;
    result = dc.Content.Find.Execute(findItem, missing, missing, missing, missing, missing, missing, Wrap, missing, "", repl, missing, missing, missing, missing);

    This tells me if the text is found or not . But I need the range of the found text-piece, because i would like to know what content control contain the word. I also need to implement a sort of MS Word "find next", when i find an occurrence of the word, check what "parentContentControl" contains the word, and continue searching until the end of the document.

    I really don't know how to get it. Would be nice if you could help me out.

    Thanks

    Matteo

    • Moved by Kristin Xie Tuesday, November 4, 2014 3:31 AM move to appropriate forum
    Monday, November 3, 2014 8:09 AM

Answers

  • Hi M4TT3,

    First, to get the find text range, you could use Range.Select() method.

    Word.Document dc = this.Application.ActiveDocument;
    
                Word.Range rng = dc.Content;
    
                dc.Activate();
    
                rng.Find.ClearFormatting();
    
                rng.Find.Wrap = 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 = Word.WdReplace.wdReplaceNone;
    
                object Wrap = Word.WdFindWrap.wdFindContinue;
    
                if (rng.Find.Execute(findItem, missing, missing, missing, missing, missing, missing, Wrap, missing, "", repl, missing, missing, missing, missing))
    
                {
    
                    MessageBox.Show("Text found.");
    
                }
    
                else
    
                {
    
                    MessageBox.Show("Text not found.");
    
                }
    
               
    
                rng.Select();

    Secondly, to continue to find the next, you need to change the content instead of using the whole document content everytime (Get remaining content range). You may use Selection.Find directly.

    Application.Selection.Find.Execute

    To get the word of parent’s content control, you could get the selected range’s ParentContentControl.

    rng.Select();
    Word.ContentControl c = rng.ParentContentControl;
    MessageBox.Show(c.Range.Text);

    There is a link about search text in documents that can help you:

    # Programmatically Search for and Replace Text in Documents

    http://msdn.microsoft.com/en-us/library/f1f367bx.aspx

    Best Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, November 5, 2014 9:42 AM
    Moderator

All replies

  • Hello Matteo,

    According to your description, your thread related to word. So i moved your thread to word for developers forum for better support.

    Have a ncie day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 4, 2014 3:30 AM
  • Hi M4TT3,

    First, to get the find text range, you could use Range.Select() method.

    Word.Document dc = this.Application.ActiveDocument;
    
                Word.Range rng = dc.Content;
    
                dc.Activate();
    
                rng.Find.ClearFormatting();
    
                rng.Find.Wrap = 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 = Word.WdReplace.wdReplaceNone;
    
                object Wrap = Word.WdFindWrap.wdFindContinue;
    
                if (rng.Find.Execute(findItem, missing, missing, missing, missing, missing, missing, Wrap, missing, "", repl, missing, missing, missing, missing))
    
                {
    
                    MessageBox.Show("Text found.");
    
                }
    
                else
    
                {
    
                    MessageBox.Show("Text not found.");
    
                }
    
               
    
                rng.Select();

    Secondly, to continue to find the next, you need to change the content instead of using the whole document content everytime (Get remaining content range). You may use Selection.Find directly.

    Application.Selection.Find.Execute

    To get the word of parent’s content control, you could get the selected range’s ParentContentControl.

    rng.Select();
    Word.ContentControl c = rng.ParentContentControl;
    MessageBox.Show(c.Range.Text);

    There is a link about search text in documents that can help you:

    # Programmatically Search for and Replace Text in Documents

    http://msdn.microsoft.com/en-us/library/f1f367bx.aspx

    Best Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, November 5, 2014 9:42 AM
    Moderator