Do a non-contiguous multi-selection of words - say, four words. Tools > Word Count returns 4.
Using the SAME selection, run:
Code:
Msgbox Selection.Words.Count
and it returns 1.
This is because VBA does not recognize a non-contiguous Selection. In fact, you can not MAKE a non-contiguous selection with VBA. You can do it manually, holding the Ctrl key, but you can't do it with VBA.
In fact, say you have a selection like:
[COLOR=red yellow]This[/color] is a [COLOR=red yellow]sentence[/color] of [COLOR=red yellow]some[/color] text.
3 words selected: This, sentence, some
In VBA, the following code:
Code:
Msgbox "There are " & Selection.Words.Count & _
" words selected. The text is " & _
Selection.Text
will return:
"There are 1 words selected. The text is some"
On a non-contiguous Selection Selection.Text returns only the LAST area selected.
The bottom line answer is Tools > Word count does in fact use Selection.Words.Count...but a different one...sort of. The "problem" lies in VBA. It does not recognize non-contiguous selection ranges. The Object Model
ONLY has one set of parameters defined for Selection.Range....a .Start, and a .End. As in:
Selection.Range.Start
Selection.Range.End
So VBA simply can not see any other parts of the Selection, except the last one. Nor can it make any. VBA can not make non-contiguous Selections.
EXCEPT...this is not totally true. back to the example above. Three words selected. VBA returns a word count of 1. But say you add an instruction to DO something with that non-contiguous Selection? Say:
Well, VBA will, in fact, action all non-contiguous parts of the Selection.
Here is another odd thing. Say you have a non-contiguous selection. You run the following:
Code:
Selection.MoveUp Unit:=wdParagraph, Count:=2, Extend:=wdMove
The selection is moved up two paragraphs.
Now...click the Undo button. Guess what happens? The previous Selection is re-selected....but ONLY the last area. It does NOT reselect non-contiguous areas.
Do the exact same thing, but manually. Have a non-contiguous selection, so something with it (say, make it bold). Click the selection up two paragraphs. Click Undo. The previous non-contiguous selection IS re-selected.
Non-contiguous selection are available through the User Interface, but not through VBA . Here is something even MORE odd. Same sort of example.
Say, three words non-contiguously selected. Run:
Code:
Selection.Font.Size = 16
Selection.MoveUp Unit:=wdParagraph, Count:=2, Extend:=wdMove
The non-contiguous selected words are font size 16, and the selection is moved up two paragraphs. Click the Undo button. Guess what?
ALL THREE non-contiguous areas are returned back to the previous state, but ONLY the last area is re-selected.
What can I say? The amazing world of Word!
As a final note, generally speaking it is preferable to use the Range object in VBA, over using the Selection object. And this is true. However, it is important to remember that while similar, Range and Selection ARE different objects.
Gerry