Ed
Thu Jan 08 07:50:52 CST 2004
That worked "Vera" good! 8>) Thanks for all your help.
Ed
"vera" <sestyd@yahoo.com> wrote in message
news:048301c3d575$8776ddf0$a401280a@phx.gbl...
> Ed,
>
> Oh, heck, I just had to try something else. Try this by
> creating a new word doc, putting some text on it,
> selecting the text, then run the sub testWordCount in your
> immediate window. And don't forget to unwrap the lines.
>
> Vera
>
>
> Function countWords() As Integer
>
> Dim i As Integer
>
> intWords = selection.Words.Count - 1
>
> Debug.Print "Word count: " & intWords
> For i = 1 To intWords
> Debug.Print "word " & i & ": " &
> selection.Words.Item(i).Text
> Next
>
> countWords = intWords
>
> End Function
>
> Sub testCountWords()
>
> Dim intWordCount As Integer
> intWordCount = countWords()
> Debug.Print "Here is the word count: " & intWordCount
>
> End Sub
>
>
> >-----Original Message-----
> >Ed,
> >
> >Whoops! Hit the wrong button!
> >I'll work out the function if you get stuck. Thanks for
> >the book tip!
> >
> >Vera
> >
> >
> >>-----Original Message-----
> >>Ed,
> >>
> >>Actually, you should just be able to pass your string to
> >>the sub in a string variable.
> >>
> >>sub countWords(ByVal strText as string)
> >>or better yet
> >>
> >>function countWords(ByVal strText as string) as integer
> >> code, code, code
> >>
> >>
> >>
> >>end function
> >>
> >>
> >>so I think you can probably pass the selection in, not
> >>sure about that
> >>
> >>if you use the function then it will return the count to
> >>you
> >>
> >>
> >>
> >>
> >>>-----Original Message-----
> >>>Vera, that was great! I was hoping there was a method
> >>that would let me do
> >>>this "inside" VBA, without having to print the string
> >>into the document, but
> >>>I can sneak it in, get what I need, and delete it back
> >>out before anyone
> >>>notices. 8>)
> >>>
> >>>(BTW, I have a book which (so it says) lists out every
> >>object, property, and
> >>>method in Word VBA. It's "Word 2000 Developer's
> >>Handbook" (ISBN
> >>>0-7821-2329-5). Halfpricecomputerbooks.com has one for
> >>24.99. It's worked
> >>>okay for me so far - as long as I know enough to know
> >>what to look up!)
> >>>
> >>>Ed
> >>>
> >>>"sestyd@yahoo.com"
> <anonymous@discussions.microsoft.com>
> >>wrote in message
> >>>news:038101c3d56a$654360b0$a501280a@phx.gbl...
> >>>> JWest & Ed,
> >>>>
> >>>> I though this was pretty interesting, and since I'm
> >>trying
> >>>> to figure out all the methods (after actually finding
> >>the
> >>>> documentation on the darned things), I wrote this sub
> >>>> which prints out the count from the count method.
> >>>>
> >>>> When I counted this paragraph by hand I got 58. The
> >>count
> >>>> method counts 59. when I actually print the words out,
> >>>> word 59 seems to be a carriage return or maybe it's
> >>empty,
> >>>> not sure yet. Word behaves the same way whether I add
> >>this
> >>>> text to a paragraph, section, or just to the document
> >>>> content. Does anyone know why that is?
> >>>>
> >>>> Anyway, try this sub (unwrap the lines). It probably
> >>will
> >>>> work consistantly if you subtract 1 from the count.
> >>>>
> >>>> Vera
> >>>>
> >>>> Sub countWords()
> >>>>
> >>>> Dim strText As String
> >>>> Dim i As Integer
> >>>>
> >>>> strText = "I get the wrong answer, because Word stops
> >at
> >>>> every punctuation mark along the way as well as
> spaces.
> >>>> So I didn't know what I was asking then - but know a
> >bit
> >>>> more now. Is there a way to count the words in a
> >string
> >>>> as Word would using Ctrl+Right Arrow?"
> >>>>
> >>>> With ActiveDocument
> >>>> .Paragraphs.Add 'add the introductory
> paragraph
> >>>> .Paragraphs(1).Range.Text = strText 'add text
> >>to it
> >>>>
> >>>> intWords = .Paragraphs(1).Range.Words.Count
> >>>>
> >>>> Debug.Print "Word count: " & intWords
> >>>> For i = 1 To intWords
> >>>> Debug.Print "word " & i & ": " & .Paragraphs
> >>>> (1).Range.Words.Item(i).Text
> >>>> Next
> >>>>
> >>>> End With
> >>>>
> >>>> End Sub
> >>>>
> >>>>
> >>>>
> >>>> >-----Original Message-----
> >>>> >I did say that it depends on what you mean by a
> >word :-
> >>)
> >>>> >
> >>>> >If you want to be sure that you are getting the same
> >>>> method as Word does,
> >>>> >then use Word's method. Insert the text into a
> >>document,
> >>>> select it, and get
> >>>> >the value of Selection.Words.Count.
> >>>> >
> >>>> >--
> >>>> >Regards
> >>>> >Jonathan West - Word MVP
> >>>> >
http://www.multilinker.com
> >>>> >Please reply to the newsgroup
> >>>> >
> >>>> >"Ed" <ed_millis@removethis.hotmail.com> wrote in
> >>message
> >>>> >news:e8$aDtV1DHA.2636@TK2MSFTNGP09.phx.gbl...
> >>>> >> The answer supplied by Jonathan West to me previous
> >>>> post is copied below:
> >>>> >>
> >>>> >> >> I have used Len(strVariable) to get the length
> >of
> >>my
> >>>> text string in
> >>>> >> >> characters. Can I also get the length
> >in "words",
> >>>> or get a count of
> >>>> >how
> >>>> >> >> many words are contained in that string?
> >>>> >>
> >>>> >> >Yes, you can, but first you need to define what
> you
> >>>> mean by a "word". For
> >>>> >> >instance is "apple-pie" one word or two?
> >>>> >> >
> >>>> >> >If you want to take the simplest case of words
> >being
> >>>> separated by spaces,
> >>>> >> >then the following line of code will do the trick
> >>>> >> >
> >>>> >> >numWords = UBound(Split(stringWithWords, " ")) + 1
> >>>> >>
> >>>> >> Unfortunately, when used with
> >>>> >> Selection.MoveRight Unit:=wdWord, Count:=numWords
> >>>> >> I get the wrong answer, because Word stops at every
> >>>> punctuation mark along
> >>>> >> the way as well as spaces. So I didn't know what
> I
> >>was
> >>>> asking then - but
> >>>> >> know a bit more now. Is there a way to count the
> >>words
> >>>> in a string as
> >>>> >Word
> >>>> >> would using Ctrl+Right Arrow?
> >>>> >>
> >>>> >> Ed
> >>>> >>
> >>>> >>
> >>>> >
> >>>> >.
> >>>> >
> >>>
> >>>
> >>>.
> >>>
> >>.
> >>
> >.
> >