none
paragraph no RRS feed

  • Question

  • When searching for a word how do you return the paragraph number and character count/position in the document? For example return 4 if the word you're searching for is in the 4th paragraph. It would also return 250 for example if it was the 250th character in the document.

        object findText = "test";

        Application.Selection.Find.ClearFormatting();

        if (Application.Selection.Find.Execute(ref findText,
            ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing))
        {
            //return paragraph no
        }

     



    Wednesday, May 4, 2011 1:08 AM

Answers

  • Hi Tessa,

    I can show you in VB.

    After finding your word, record the starting/ending position with this command.

    Dim sPos as Long = wApp.Selection.Range.Start

    Dim ePos as Long = wApp.Selection.Range.End

    To record the paragraph you’ll use a range command again.

    Dim paraRng as Range = wApp.ActiveDocument.Range(Start:=0, End:=sPos)

    Dim paraCnt as Integer = paraRng.Paragraphs.Count


    Regards, Rich
    • Marked as answer by Tessa75 Wednesday, May 4, 2011 3:29 AM
    Wednesday, May 4, 2011 2:12 AM
  • Hi Tessa,

    Try something based on the following vba code:

    Sub Demo()
    Dim RngDoc As Word.Range, RngPara As Word.Range
    With ActiveDocument
      Set RngDoc = .Range(0, Selection.Range.End)
      With Selection
        Set RngPara = .Paragraphs.Last.Range.Characters.First
        RngPara.End = .Range.End
      End With
      MsgBox "Last Paragraph selected is #:" & .Paragraphs.Count & " in document." & vbCr & _
        "Postion in document of last character selected: " & .Characters.Count & vbCr & _
        "Postion in paragraph of last character selected: " & RngPara.Characters.Count
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]
    • Marked as answer by Tessa75 Wednesday, May 4, 2011 3:29 AM
    Wednesday, May 4, 2011 3:05 AM

All replies

  • Hi Tessa,

    I can show you in VB.

    After finding your word, record the starting/ending position with this command.

    Dim sPos as Long = wApp.Selection.Range.Start

    Dim ePos as Long = wApp.Selection.Range.End

    To record the paragraph you’ll use a range command again.

    Dim paraRng as Range = wApp.ActiveDocument.Range(Start:=0, End:=sPos)

    Dim paraCnt as Integer = paraRng.Paragraphs.Count


    Regards, Rich
    • Marked as answer by Tessa75 Wednesday, May 4, 2011 3:29 AM
    Wednesday, May 4, 2011 2:12 AM
  • Hi Tessa,

    Try something based on the following vba code:

    Sub Demo()
    Dim RngDoc As Word.Range, RngPara As Word.Range
    With ActiveDocument
      Set RngDoc = .Range(0, Selection.Range.End)
      With Selection
        Set RngPara = .Paragraphs.Last.Range.Characters.First
        RngPara.End = .Range.End
      End With
      MsgBox "Last Paragraph selected is #:" & .Paragraphs.Count & " in document." & vbCr & _
        "Postion in document of last character selected: " & .Characters.Count & vbCr & _
        "Postion in paragraph of last character selected: " & RngPara.Characters.Count
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]
    • Marked as answer by Tessa75 Wednesday, May 4, 2011 3:29 AM
    Wednesday, May 4, 2011 3:05 AM
  • Thank you!

    Wednesday, May 4, 2011 3:29 AM