Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA in Word, collection of words 1

Status
Not open for further replies.

jslmvl

Vendor
Jan 26, 2008
268
GB
Hi,

In Ms Word, what is the collection of words delimited by spaces instead by comma, dot or something else.

Cheers.
 




Hi,

You ought to check out the MS Word Object Model in HELP.

The Words Collection is not something that is delimited. It is an OBJECT that has various properties and methods.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Skip is correct in saying the Words collection does not have delimiters.

However...
Help said:
Each item in the Words collection is a Range object that represents one word. There is no Word object.
My bolding.

This is something that is not commonly understood. There is no Word object. The Words collection is a collection of Ranges.

Further, Range.Words - as a property - includes periods, and paragraphs marks as "words". So any count of them - or any other action - will include (for example) the period at the end of a sentence as a separate "word".

Ranges passed through the internal Sub WordCount do not do this. This internal subroutine does have delimiter logic.

Perhaps if you explained what you are trying to do, we may be able to suggest something.

faq219-2884

Gerry
My paintings and sculpture
 
And just to add to the conundrum, the number 123,456,789.0 id treated as 4 words with the words collection.

Cheers

[MS MVP - Word]
 




I counted EIGHT...
[tt]
1. 123
2. ,
3. 456
4, ,
5. 789
6. .
7. 0
8. LINE FEED
[/tt[


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hi Skip,

Yes - I forgot that it counts the separators as words in their own right. That just makes looking for non-space separators more of a challenge.

Cheers

[MS MVP - Word]
 
Just to reiterate what I previously posted...

123,456,789.0<p> - terminated by a paragraph mark

If selected;
Code:
Sub CountEm()
MsgBox Selection.Range.ComputeStatistics(wdStatisticWords) _
   & vbCrLf & Selection.Range.Words.Count
End Sub
displays:

1
8

This demonstrates the differences. The method ComputeStatistics does pass the range through the internal Sub WordCount. Thus, it does use delimiters, and returns a count of 1.

Range.Words.Count does NOT pass through WordCount, and therefore does NOT use delimiters. This means commas (etc.) are counted as "words". Thus it returns a count of 8.

Just to change things, using spaces...

123 456 789.0<p>

The code would display:

3
6

ComputeStatistics:

123 word_1
456 word_2
789.0 word_3

The use of delimiters means the ".0" is included with 789; and the paragraph mark is ignored.

Words.Count:

123 word_1
space word_2
456 word_3
space word_4
789.0 word_5
<p> word_6

Bottom line is that if you are careful, and VERY explicit in your logic, you can get accurate counts of "words". However, you really do have to be very very clear.

faq219-2884

Gerry
My paintings and sculpture
 
Thanks a lot for all your helps.

fumei's
Selection.Range.ComputeStatistics(wdStatisticWords)
is close to what I am looking for....
However, that is only count the number of "words" like
Selection.Words.Count (it is not works)
while I need
Selection.Words(i), i=1,2,...
No such thing in MS Word?
 
Ummm, jslmvl, I fear you are being a "bit" too cryptic about what you need!
Leave away the code for a second and just explain what exactly you are trying to perform.
:)

Greets,
MiS

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Yes. You need to clearly, explicitly - and hopefully fairly simply - explain EXACTLY what you want to try and do.

I have explained what the problem is with determining "words", and how Word considered "words". YOU have to work with that.

If I understand correctly:

while I need
Selection.Words(i), i=1,2,...

seems to mean you want to (somehow) go through each "word".

If you read my post, the code Selection.Range.ComputeStatistics(wdStatisticWords) will return a number - not the "word(s)" themselves - filtered through the delimiters.

Again, if you are careful, you will be able to use a variable (i), in conjunction with that number, to get those "words" in some sort of incremental loop - your i=1,2,...

Here is an example. Say you have the string

The quick brown fox jumps over the lazy dog.

Notice the extra spaces between "the" and "lazy". Say the string is Selected...
Code:
Dim j As Long

For j = 1 To Selection.Range.ComputeStatistics(wdStatisticWords)
   MsgBox Selection.Range.Words(j)
Next
The result will be:

The
quick
fox
jumps
over
the
lazy
dog

The "." at the end (while considered a "word" by Range.Words) will NOT be returned. Neither will the extra spaces. This way you can get the individual "words" minus the spaces.

Hope this helps.

faq219-2884

Gerry
My paintings and sculpture
 
Why not simply use the Split function (and probably a For Each iteration) ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Yes you could, but unfortunately we do not know exactly what is the structure of the string being dealt with...or even why...or even what is wanted to be done with the "words".

Using a For Each can be very tricky with "words", for the reasons I have stated. There is no word object. So "Each" is slippery.

They are ranges, and how those ranges are determined varies depending on the method used.

faq219-2884

Gerry
My paintings and sculpture
 
Gerry, I meant something like For Each strW In Split(...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top