none
Word: Find all sentences containing a specific word

    Question

  • Hello,

    How can I browse through all sentences of a word document when I am performing a search in a document.Content range ?
    I would like to find all sentences containing a specific word, and then list them somewhere. How should I proceed ?

    Regards,
    TheParadoX
    Tuesday, June 24, 2008 7:57 PM

Answers

  • Mmm. I guess the first thing you need to decide is whether you want to loop through all sentences in the document (pseudocode):

    'Each sentence in the collection returns a Range object

    For Each sent in Document.Content.Sentences

    If sent.Text.Contains("your term") Then

     

    Or if you want to find the term, then pick up the sentence from the found range (pseudocode):

     

    Word.Range rngFind = Document.Content

    rngFind.Text = "your term"

    If rngFind.Execute Then

    Word.Range sent = rngFind.Sentences(1)

    Wednesday, June 25, 2008 9:17 AM
    Moderator

All replies

  • Mmm. I guess the first thing you need to decide is whether you want to loop through all sentences in the document (pseudocode):

    'Each sentence in the collection returns a Range object

    For Each sent in Document.Content.Sentences

    If sent.Text.Contains("your term") Then

     

    Or if you want to find the term, then pick up the sentence from the found range (pseudocode):

     

    Word.Range rngFind = Document.Content

    rngFind.Text = "your term"

    If rngFind.Execute Then

    Word.Range sent = rngFind.Sentences(1)

    Wednesday, June 25, 2008 9:17 AM
    Moderator
  • Hello,

    Thank you. I will choose the first solution, as I have more than word to check the presence for.

    Now a specific technical question: How do I loop through the sentences ? The compiler tells me this: 'Microsoft.Office.Interop.Word.Range' does not contain a public definition for 'GetEnumerator'

    Thanks for your help !
    TheParadoX


    Wednesday, June 25, 2008 2:30 PM
  •  TheParadoX wrote:
    Hello,
    Now a specific technical question: How do I loop through the sentences ? The compiler tells me this: 'Microsoft.Office.Interop.Word.Range' does not contain a public definition for 'GetEnumerator'

     

    This works just fine for me:

     

    public void LoopSentences(Office.IRibbonControl ctl)

    {

    Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;

    foreach (Word.Range sent in doc.Sentences)

    {

    if (sent.Text.Contains("test"))

    {

    System.Windows.Forms.MessageBox.Show(sent.Text);

    }

    }

    }


     

Wednesday, June 25, 2008 3:28 PM
Moderator
  • Thank you very much for your fast support, I appreciate it.
    I'll probably be back with other issues later Big Smile

    regards
    Wednesday, June 25, 2008 3:34 PM