Steve,
Expanded your suggestion a bit as I wouldn't expect you would want to
format numbers like phone numbers 1-800-867-5309, SSN's 123-45-6789, or
Serial Numbers
123SEWR3432432143SWQ21, etc.
Sub CommaFormatNumbers()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "([0-9]{4,})"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
While .Execute
Select Case Asc(oRng.Characters.Last.Next)
Case 7, 9, 10, 11, 13, 32
On Error GoTo Handler
Select Case Asc(oRng.Characters.First.Previous)
Case 7, 9, 10, 11, 13, 32
Proceed:
oRng = Format$(oRng, "#,##0")
oRng.Collapse wdCollapseEnd
Case Else
oRng.Collapse wdCollapseEnd
End Select
Case Else
'Do Nothing
End Select
Wend
End With
Exit Sub
Handler:
Resume Proceed
End Sub
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
Steve said:
I suspect there is a Find/Replace that would be more direct but this
macro should do what you are asking.
_ __ ______ __________________________
Sub AddCommasToNumbers()
Dim rngOriginal As Range
Dim strTemp As String
Application.ScreenUpdating = False
Set rngOriginal = Selection.Range
ActiveDocument.Range(0, 0).Select
With Selection.Find
.ClearFormatting
.Text = "<[0-9]{4,}>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
Do While .Execute
strTemp = Selection.Text
Selection.Text = Format$(strTemp, "#,##0")
Selection.Collapse wdCollapseEnd
Loop
End With
rngOriginal.Select
Application.ScreenUpdating = True
End Sub
______________________________________________
Steve Yandl
sharris said:
When I am typing a document in Word (let's say a letter) and I type
numbers
with a comma such as 1,158, I use the number pad but have to go over
to the
letter pad for the comma. There should be a comma in the number pad.
Is there
any way to format with auto correct with the numbers being unknown,
to always
insert a comma after every number that is followed by three numbers
(1,111,111 or 1,158)? Or is there some other way that I don't know
about, such as automatically invoking a macro?