Character count - VBA and Word

S

Scott P

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.
 
J

Jezebel

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
 
S

Scott P

Thank you for your reply and help -- I appreciate the explanation regarding
the paragraph mark and cell markers and your providing the below code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top