gmaxey
11-19-2012, 07:43 AM
I think I've discovered a bug in Word's word count feature. A group of words separated by em or en spaces are seen as one word. See: http://answers.microsoft.com/en-us/office/forum/officeversion_other-word/word-count-bug-emen-space/de96c726-fc84-45dd-8cf8-ae2a9cce4026
It seems that I can work around this using:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim strTemp As String
Dim lngCount As Long
strTemp = Selection.Text
strTemp = Replace(strTemp, Chr(160), " ") 'non-breaking space.
strTemp = Replace(strTemp, Chr(151), " ") 'em dash
strTemp = Replace(strTemp, Chr(150), " ") 'en dash
strTemp = Replace(strTemp, ChrW(8195), " ") 'em space
strTemp = Replace(strTemp, ChrW(8194), " ") 'en space
strTemp = Replace(strTemp, ChrW(8197), " ") '1/4 em space
strTemp = Replace(strTemp, Chr(9), " ") 'vbTab
strTemp = Replace(strTemp, Chr(11), " ") 'vbLineBreak
Do While InStr(strTemp, " ") > 0
strTemp = Replace(strTemp, " ", " ")
Loop
lngCount = UBound(Split(strTemp, Chr(32))) + 1
MsgBox "The selection contains : " & lngCount & " word(s)."
End Sub
Any suggestions for streamlining the repeated "Replace" steps?
It seems that I can work around this using:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim strTemp As String
Dim lngCount As Long
strTemp = Selection.Text
strTemp = Replace(strTemp, Chr(160), " ") 'non-breaking space.
strTemp = Replace(strTemp, Chr(151), " ") 'em dash
strTemp = Replace(strTemp, Chr(150), " ") 'en dash
strTemp = Replace(strTemp, ChrW(8195), " ") 'em space
strTemp = Replace(strTemp, ChrW(8194), " ") 'en space
strTemp = Replace(strTemp, ChrW(8197), " ") '1/4 em space
strTemp = Replace(strTemp, Chr(9), " ") 'vbTab
strTemp = Replace(strTemp, Chr(11), " ") 'vbLineBreak
Do While InStr(strTemp, " ") > 0
strTemp = Replace(strTemp, " ", " ")
Loop
lngCount = UBound(Split(strTemp, Chr(32))) + 1
MsgBox "The selection contains : " & lngCount & " word(s)."
End Sub
Any suggestions for streamlining the repeated "Replace" steps?