Results 1 to 5 of 5
  1. #1
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Leigh on Sea, Essex, England
    Posts
    263
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Selecting words with VBA (XP/2002)

    Two related questions:

    1. How does Words.Count count words? For example, "4+5/16" gets counted as 5 words (I'm assuming that this is "4", "+", "5", "/" and "16", but I could be wrong).

    2. How would I go about picking up the whole word under a cursor, regardless of how much is selected?

    I'm trying to write a macro that will format fractions - in the example above, the + separates the whole number from the fraction (it will get deleted) and the numbers to the left of the / will be superscript while those to the right will be subscript. Assuming I cana read the text, the rest is easy, but I want to make sure that I have the whole "fraction" selected before I start, plus I want to be able to perform the operation on all the words in a selection one at a time if more that one word is selected. It's this selection bit that's giving me the grief - Excel VBA I understand, but Word VBA doesn't seem as intuitive and I don't really know where to start.

    Thanks.

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

    Re: Selecting words with VBA (XP/2002)

    1. I'm not sure about the first question. Word 97 counts "4+5/16" as one word unless I put in spaces "4 + 5 / 16". Then it counts as 5 words.

    2. You might try

    Dim strDelim As String
    strDelim = " " & vbCr & vbTab
    Selection.MoveStartUntil strDelim, wdBackward
    Selection.MoveEndUntil strDelim, wdForward

    Here, a space, line feed and tab are used as delimiter. If your formula may be followed by a comma or period, add those:

    strDelim = " ,." & vbCr & vbTab

    and you can add other delimiters, of course.

    This doesn't work for a "word" immediately before or after a table.

  3. #3
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Leigh on Sea, Essex, England
    Posts
    263
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Selecting words with VBA (XP/2002)

    Hans,

    Thanks for (2) - I'll give it a go.

    As for (1), you get different answers depending on how you ask the question. For example, in a blank document, enter the phrase

    <pre>The journey will take 1+1/2 hours.</pre>


    Using Tools | Word Count correctly gives you 6 words. Now create a macro and run the code:

    <pre>MsgBox ActiveDocument.Words.Count</pre>


    Twelve. Not six, twelve. (Specificially "The ", "journey ", "will ", "take ", "1", "+", "1", "/", "2 ", "hours" and "." plus one for the paragraph mark / carriage return, which you can find by looking at each of the elements of the Words collection in turn).

    I've either misunderstood the Words collection, or a very large scream is about due here!

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

    Re: Selecting words with VBA (XP/2002)

    Hi Stuart,

    You're right about (1). I had only tried Tools/Count words... In Word 97, Words.Count in VBA acts the same way you described for XP. Very strange.

    Regards,
    Hans

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Selecting words with VBA (XP/2002)

    It took a while to find the equivalent VBA code for the Word Count dialog, but here it is:

    lngWordCount = Selection.Range.ComputeStatistics(wdStatisticWords )

Posting Permissions

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