Thread: [Solved] Number and date formatting
View Single Post
 
Old 01-29-2013, 09:12 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,359
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi dmarie,

You could use a macro like the following for both problems (the second one doesn't really require a macro, but it can be easily incorporated into the one that is required for the first problem).
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim StrTxt As String
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    'Fix SSNs
    .Text = "([0-9]{3})([0-9]{2})([0-9]{4})"
    .Replacement.Text = "\1-\2-\3"
    .Execute Replace:=wdReplaceAll
    'Fix Date ranges
    .Text = "[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}-[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}"
    .Replacement.Text = ""
    .Execute
  End With
  Do While .Find.Found
    StrTxt = Format(Trim(Split(.Text, "-")(0)), "MMMM D, YYYY")
    Select Case Trim(LCase(.Words.First.Previous.Previous.Words.First))
      Case "between": StrTxt = StrTxt & " and "
      Case "from": StrTxt = StrTxt & " to "
      Case "of": StrTxt = StrTxt & " through "
    End Select
    StrTxt = StrTxt & Format(Trim(Split(.Text, "-")(1)), "MMMM D, YYYY")
    .Text = StrTxt
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
You'll note that I've included a test for date ranges prefixed by 'from' - just to show how easily additional parameters can be accomodated.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote