Consulting

Results 1 to 8 of 8

Thread: Index Value of a Word in a Paragraph

  1. #1
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location

    Index Value of a Word in a Paragraph

    Is there a way to get the index value of a word in a paragraph. Similar to

    Sub test()
        Dim wapp As Word.Range
       
        Set wapp = ActiveDocument.Range
    
        wapp.Paragraphs(1).Range.Words(4).Select 'Highlights the 4th word in the paragraph
    End Sub
    but in reverse? I know I can just loop through the paragraph but it just seems like there would be a way of getting the index number of the word in the paragraph.

  2. #2
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    I found the following that seems to work. Is there another way of doing it that might be better?
    Sub test()
     Dim oRng As Range
     Dim WrdInd As String
     Set oRng = Selection.Range
     oRng.Expand Unit:=wdParagraph
     Set oRng = ActiveDocument.Range( _
     Start:=oRng.Start, _
     End:=Selection.Start)
     WrdInd = oRng.ComputeStatistics(wdStatisticWords) + 1
    End Sub

  3. #3
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You could use:
    Sub Demo()
     Dim Rng As Range, i as Long
     Set Rng = Selection.Range.Paragraphs.First.Range
     Rng.End = Selection.Start
     i = Rng.ComputeStatistics(wdStatisticWords)
     End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  4. #4
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    That works great! Thanks again for your help.

  5. #5
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    I have a bit of an issue. The code you gave me doesn't count non-letters (which I would expect) but
     wapp.Paragraphs(1).Range.Words(4).Select
    counts non-letters. So if I was to use
     
    Sub Demo() 
        Dim Rng As Range, i As Long 
        Set Rng = Selection.Range.Paragraphs.First.Range 
        Rng.End = Selection.Start 
        i = Rng.ComputeStatistics(wdStatisticWords)
       Rng.Paragraphs(1).Range.Words(i).Select
    End Sub
    it highlights the wrong word. Is there a way of doing this where either option counts the same?

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You could use something like:
    Sub Demo()
        Dim Rng As Range, i As Long, j As Long
        With Selection
          Set Rng = .Paragraphs.First.Range
          Rng.End = .Start
          i = Rng.ComputeStatistics(wdStatisticWords)
          With .Paragraphs.First.Range
            For j = i To .Words.Count
              Rng.End = .Words(j).End
              If Rng.ComputeStatistics(wdStatisticWords) = i Then
                Rng.Words.Last.Select
                Exit For
              End If
            Next
          End With
        End With
    End Sub
    but this seems like a whole lot of circumlocution for something you could achieve with a single line of code:
    Selection.Words.First.Select
    And then there's the question of why you're selecting anything at all, since you rarely need to do that with VBA.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    "Selection" was just for ease of discussing the problem. I'm using .find of a range to get the word. I then need to get the previous 3 words in front of what is found. I'm guessing it might just be easier to just do a loop using
    Rng.Paragraphs(1).Range.Words(i).text
    to search for the correct string. What do you think?

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Why don't you use Find for the entire string? If you don't know what the previous two words are, use wildcards.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •