PDA

View Full Version : Word Count Bug, em/en space



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?

Paul_Hossler
11-19-2012, 09:30 AM
http://www.aivosto.com/vbtips/stringopt.html#replace



The following tip might be obvious, but it wasn't to us. It makes no sense to call Replace if you're not likely to replace anything. Replace runs slowly. Replace always creates a copy of the input string, even if no replacement occurs, and making the copy is slow. If a replacement is unlikely, verify first (with InStr (http://www.aivosto.com/vbtips/instr.html) or InStrB, for example) that there is something you need to replace.
If InStr(Text$, ToBeReplaced$) <> 0 Then Text$ = Replace(Text$, ToBeReplaced$, "xyz")End IfIf a replacement is likely or certain to occur, there is no need to call InStr. It just adds an extra burden.


so maybe ...


If InStr(strTemp, Chr(160)) <> 0 then strTemp = Replace(strTemp, Chr(160), " ")


Paul

gmaxey
11-19-2012, 11:37 AM
Paul,

Thanks!!