Results 1 to 7 of 7
  1. #1
    Star Lounger
    Join Date
    Feb 2001
    Location
    Auckland, North Island, New Zealand
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    return selected word number in vba (2003)

    Is there a expression/function which will return number of the word that the insertion point is in?

    For example if the entire document has only the following words in it,

    The quick brown fox jumps over the lazy dog.

    and I have the insertion point in "lazy", will return 8?

    Thanks

    JimB
    "

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: return selected word number in vba (2003)

    As far as I know, there is no built-in method for this. The following function will return the index of the first word in the specified range, but it will be extremely slow, even for short documents:

    Function WordIndexOf(rng As Range) As Long
    Dim lngStart As Long
    Dim i As Long
    lngStart = rng.Words(1).Start
    For i = 1 To ActiveDocument.Words.Count
    If ActiveDocument.Words(i).Start = lngStart Then
    WordIndexOf = i
    Exit For
    End If
    Next i
    End Function

    Use like this:

    Debug.Print WordIndexOf(Selection.Range)

  3. #3
    Star Lounger
    Join Date
    Feb 2001
    Location
    Auckland, North Island, New Zealand
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: return selected word number in vba (2003)

    Thanks Hans
    It does the job but as you say is so slow it is really unusable for what I want'ed. It's a pity that MS didn't build somethign in for this.
    Regards
    Jim.

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: return selected word number in vba (2003)

    Is this any quicker?

    StuartR
    <code>
    Function WordNumber(rng As Range) As Long
    Dim rngFromStart As Range
    Set rngFromStart = rng.Duplicate
    rngFromStart.Start = ActiveDocument.Content.Start
    rngFromStart.End = rng.Start
    WordNumber = rngFromStart.ComputeStatistics(Statistic:=wdStatis ticWords)
    Set rngFromStart = Nothing
    End Function
    </code>

  5. #5
    Star Lounger
    Join Date
    Feb 2001
    Location
    Auckland, North Island, New Zealand
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: return selected word number in vba (2003)

    Thanks Stuart,

    Yes, many times quicker and therefore usable. The only issue to be resolved is that the two functions give significantly different counts! I'll check out why and try to find the one which suits best.

    Thanks

    Regards

    Jim

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: return selected word number in vba (2003)

    In fact, Stuart's function is not only incomparably faster, but its result is also much better. The Words collection counts punctuation as separate words, so the result of my function does not crorespond with our normal notion of the word number. So use Stuart's function!

  7. #7
    Star Lounger
    Join Date
    Feb 2001
    Location
    Auckland, North Island, New Zealand
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: return selected word number in vba (2003)

    Yes, I ran into this problem of punctuation being counted as words before. If it's of any use to anybody this function checks for whether something is a real word in a context similar to the one we are talking about here:

    Function IsAWord(ByVal strWordToTest As String) As Boolean
    'is this a true word (English). ie. does it consist of characters only.
    Dim intCharCount As Integer
    Dim intCounter As Integer
    Dim intCharAsc As Integer

    intCharCount = Len(strWordToTest)
    For intCounter = 1 To intCharCount
    intCharAsc = Asc(Mid(strWordToTest, intCounter, 1))
    'check lower case - more common
    If intCharAsc >= 97 And intCharAsc <= 122 Then
    IsAWord = True
    Exit For
    End If
    'check ucase
    If intCharAsc >= 65 And intCharAsc <= 90 Then
    IsAWord = True
    Exit For
    End If
    Next
    End Function

    Thanks and regards

    Jim B

Posting Permissions

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