none
Get the complete sentence where Words Find a word. RRS feed

  • Question

  • I am finding a word in MS Word document using VSTO Find object. Find object is working fine and return TRUE on FIND.EXECUTE method but I am not able to get the complete sentence which contain this word into the document. For example, following are two sentences in the document.

    When I went to the doctor he was not present. He came at 6:00 PM.

     

    So now if I search "doctor" word within the document, Find.Execute return true but I don't know how can I get the complete sentence (like "When I went to the doctor he was not present.") where this word used. Here is my code snippet.

     

    object oTrue = true;
    object oFalse = false;
    object fText = "<(doctor)";
    Word.Find fnd = Application.ActiveDocument.Content.Find;
    fnd.Forward = true;
    bool findWord = fnd.Execute(ref fText, ref oTrue, ref oFalse, ref oTrue);
    while (findWord)
    {
        SentenceList.Items.Add(Application.Selection.Sentences[1].Text);
        findWord = fnd.Execute(ref fText, ref oTrue, ref oFalse, ref oTrue);
    }
    

    Any help please?

    Tuesday, May 10, 2011 1:03 PM

Answers

  • Hi Irfan

    you should declare a Range object and assign that to the Document.Content range. Use Find.Execute with this Range object. If what you're searching for is "found", then the Range object is changed to match what was found (it's no longer the entire document, but the place where fText was found). From there you can get the sentence. Something like this (pseudo-code, untested):

    object oTrue = true;
    object oFalse = false;
    object fText = "<(doctor)";<br/>Word.Range rngFind = Application.ActiveDocument.Content.
    Word.Find fnd = rngFind.Find;
    fnd.Forward = true;
    bool findWord = fnd.Execute(ref fText, ref oTrue, ref oFalse, ref oTrue);
    while (findWord)
    {
      Word.Sentence s = rngFind.Sentences[1];<br/>}
    

    Cindy Meister, VSTO/Word MVP
    • Marked as answer by M. Irfan Wednesday, May 11, 2011 12:38 PM
    Tuesday, May 10, 2011 4:21 PM
    Moderator

All replies

  • Hi Irfan

    you should declare a Range object and assign that to the Document.Content range. Use Find.Execute with this Range object. If what you're searching for is "found", then the Range object is changed to match what was found (it's no longer the entire document, but the place where fText was found). From there you can get the sentence. Something like this (pseudo-code, untested):

    object oTrue = true;
    object oFalse = false;
    object fText = "<(doctor)";<br/>Word.Range rngFind = Application.ActiveDocument.Content.
    Word.Find fnd = rngFind.Find;
    fnd.Forward = true;
    bool findWord = fnd.Execute(ref fText, ref oTrue, ref oFalse, ref oTrue);
    while (findWord)
    {
      Word.Sentence s = rngFind.Sentences[1];<br/>}
    

    Cindy Meister, VSTO/Word MVP
    • Marked as answer by M. Irfan Wednesday, May 11, 2011 12:38 PM
    Tuesday, May 10, 2011 4:21 PM
    Moderator
  • Hi M. Irfan,

    After you have located the Word then a range command such as this VB example will select the sentence in which that Word is contained.

        Set rng = Selection.Range
        With rng
            .MoveStart unit:=wdSentence, Count:=-1
            .MoveEnd unit:=wdSentence, Count:=1
            .Select
        End With

    Hope this helps


    Regards, Rich
    Tuesday, May 10, 2011 5:02 PM
  • Thank you Cindy.
    Wednesday, May 11, 2011 12:37 PM