Howdy Hallel,
The StrReverse works fine, although there are a couple of
small problems. For one thing, if you use a "For Each ...
Next" loop to iterate through the Words collection, then
changing the contents of a word will cause the changed
word to re-appear in the next iteration. This sets up an
infinite loop. The second problem is that the ranges in
the Words collection include all trailing spaces. If you
do a simple StrReverse then you will put all the spaces
from the right hand side onto the left.
The following code addresses those issues.
Marty
(as usual, the email address is a bit altered to avoid
spam bots, the actual address should be msn not nsm.)
Option Explicit
Public Sub InvertAllNumbers()
Dim _
blnInverted As Boolean, _
lngLongWord As Long, _
lngNoSpaces As Long, _
rngNext As Range, _
strFollow As String, _
strNumber As String
' Purpose: examines each word in a document to see if
' it is numeric. If the word is numeric,
' then the order of the numerals is reversed
' (123 becomes 321).
' Comment: Changing the text from 123 to 321 affects
' the "For Each ... Next" loop, so that the
' range containing 321 is what appears in
' the next iteration. Left alone, this will
' cause an infinite loop in which 123 is
' changed to 321 and then back to 123. A
' boolean is used to note when a number has
' been reversed. The boolean directs an
' extra "For Each ... Next" loop
' after a reversal.
' Comment: Each range in the Words collection
' includes the space(s) following the
' text (if any). Simply inverting this
' range will cause the following space to be
' flipped over to the leading edge.
' As a result, a double-space
' is produced on the leading edge of the
' inverted number and inverted number
' is run into the following word
' To prevent this, the number is trimmed of
' spaces and a variable, strFollow, is used
' to track following space(s).
blnInverted = False
For Each rngNext In ActiveDocument.Words
If Not blnInverted Then
strNumber = rngNext.Text
If IsNumeric(strNumber) Then
If Right(strNumber, 1) = " " Then
lngLongWord = Len(strNumber)
strNumber = RTrim(strNumber)
lngNoSpaces = _
lngLongWord - Len(strNumber)
strFollow = Space(lngNoSpaces)
Else
strFollow = ""
End If
rngNext.Text = _
StrReverse(strNumber) & strFollow
blnInverted = True
End If
Else
blnInverted = False
End If
Next rngNext
End Sub