Hi,
I am trying to count the number of characters in a MS Word document. My
document contains the text "Hello world." with a paragraph mark following the
text. The "Word Count" feature within MS Word indicates that my document
contains 12 characters (with spaces).
The following VBA code applied to this document, however, indicates that my
document contains 13 characters:
Sub CharacterCode()
Dim lngChar As Long
lngChar = ActiveDocument.Characters.Count
Debug.Print "lngChar = " & lngChar
End Sub
Why is there a difference between these character count values and, perhaps
more specifically, why is VBA indicating that there is an extra character in
my document as compared in MS Word's native "Word Count" feature?
Thank you in advance for your input on this topic.
Jezebel - 23 Feb 2006 06:32 GMT
The difference is that VBA includes the paragraph mark. If you have tables
it will also include cell markers.
Try this to see what's happening --
Dim pChar As Range
For Each pChar In ActiveDocument.Characters
Debug.Print Asc(pChar)
Next
> Hi,
>
[quoted text clipped - 25 lines]
>
> Thank you in advance for your input on this topic.
Scott P - 24 Feb 2006 01:36 GMT
Thank you for your reply and help -- I appreciate the explanation regarding
the paragraph mark and cell markers and your providing the below code.
> The difference is that VBA includes the paragraph mark. If you have tables
> it will also include cell markers.
[quoted text clipped - 35 lines]
> >
> > Thank you in advance for your input on this topic.