Character Counting Macro

P

Phil H

Could someone modify this Excel macro to count all characters and spaces in
the current word document. Appreciate any help.

Sub CountCellCharacters()
Dim StrinLen As Long
Dim EmptySpaces As Long
Dim Cellref As String
Cellref = Trim(ActiveCell.Value)
StrinLen = Len(Cellref)
EmptySpaces = StrinLen - Len(Replace(Cellref, " ", ""))
MsgBox "Total Cell Characters and Spaces: " & StrinLen & Chr(10) ' & "Empty
Spaces: " & EmptySpaces
End Sub
 
D

DaveLett

Hi Phil,
Without changing any of the variable names, I think this is close to what
you're looking for:

Dim StrinLen As Long
Dim EmptySpaces As Long
Dim Cellref As String
Cellref = ActiveDocument.Range.Text
StrinLen = Len(Cellref)
EmptySpaces = StrinLen - Len(Replace(Cellref, " ", ""))
MsgBox "Total Characters and Spaces: " & StrinLen & Chr(10) & "Empty Spaces:
" & EmptySpaces

HTH,
Dave
 
P

Pesach Shelnitz

Hi,

In Excel, Chr(10) inserts a line break. In Word, the equivalent is Chr(11).
I suggest that you change Chr(10) to Chr(11) in the Word macro.
 
P

Phil H

Dave, this is working, sort of. The message reads:

Total Characters and Spaces: 11
EmptySpaces: " & EmptySpaces

Also in this test, I entered 8 characters and 2 spaces, but the macro says 11.

R, Phil
 
D

DaveLett

Hi Phil,

Try the following:

Dim StrinLen As Long
Dim EmptySpaces As Long
Dim Cellref As String
Cellref = ActiveDocument.Range.Text
StrinLen = Len(Cellref) - ActiveDocument.Paragraphs.Count
EmptySpaces = Len(Cellref) - Len(Replace(Cellref, " ", ""))
MsgBox "Total Characters and Spaces: " & StrinLen & Chr(10) & "Empty
Spaces:" & EmptySpaces

The line starting with MsgBox should all be on one line.

Also, Chr(10) is the linefeed character for all VBA.

Dave
 
P

Phil H

Thanks, Dave - working just as needed.

DaveLett said:
Hi Phil,

Try the following:

Dim StrinLen As Long
Dim EmptySpaces As Long
Dim Cellref As String
Cellref = ActiveDocument.Range.Text
StrinLen = Len(Cellref) - ActiveDocument.Paragraphs.Count
EmptySpaces = Len(Cellref) - Len(Replace(Cellref, " ", ""))
MsgBox "Total Characters and Spaces: " & StrinLen & Chr(10) & "Empty
Spaces:" & EmptySpaces

The line starting with MsgBox should all be on one line.

Also, Chr(10) is the linefeed character for all VBA.

Dave
 
P

Phil H

That was really what I was trying to get to - attached to a macro button so I
didn't have to go through all the clicks. Nice. Thanks again.
 
F

Fumei2 via OfficeKB.com

Alternative.

Option Explicit

Sub CountEmDano()
Dim r As Range
Set r = ActiveDocument.Range

MsgBox "Total Characters including Spaces: " & _
r.ComputeStatistics(wdStatisticCharactersWithSpaces) & _
vbCrLf & "Empty Spaces: " & _
r.ComputeStatistics(wdStatisticCharactersWithSpaces) - _
r.ComputeStatistics(wdStatisticCharacters)
End Sub

A document with:

Yadda yadda yadda.


will return:

Total Characters including Spaces: 18
Empty Spaces: 2

Phil said:
That was really what I was trying to get to - attached to a macro button so I
didn't have to go through all the clicks. Nice. Thanks again.
[quoted text clipped - 53 lines]
 

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