bottom
Great WordTips!
         
Your e-mail address is safe!
Close Note

Tips.Net > WordTips Home > Creating Macros > Dates and Times > Changing the Format of Existing Dates

Changing the Format of Existing Dates

Summary: There are a myriad of ways in which a date can be formatted—day first, month first, number of digits in the year, etc. If you want to change the way a date is formatted once it is in your document, how you do so depends on the way in which the date was inserted to begin with. (This tip works with Microsoft Word 97, Word 2000, Word 2002, Word 2003, and Word 2007.)

Aileen works with a lot of documents that have the date in a numeric format, such as 4/22/08. She needs to convert these dates to a different format, specifically April 22, 2008. She wonders if there is an easy way to make the change without needing to retype each date.

The answer depends, in large part, on the nature of the date you are changing. Dates in a Word document can either be straight text or a field. You can tell if the date is a field by simply moving the insertion point somewhere inside the date. If it is a field, then the entire date should be shaded in light gray once the insertion point is within the date.

If the date is a field, then you can right-click on the date and choose Edit Field from the resulting Context menu. You can then modify the field, including selecting a different date format in the Field Properties list.

If the date is regular text (not a field), then you need to look to a different solution. It is possible to go through the document and manually retype all the dates, but Aileen already found out that such an approach is tedious. This is where a macro can come in handy: to cure the tedium by programmatically doing what you would otherwise do by hand.

The following macro will step through a document, searching for all dates in the format m/d/yyyy. (There can be either one or two digits for either the month or day, but must be four digits for the year.) If a date matching this pattern is found, it is converted to the format mmmm d, yyyy.

Sub GetDateAndReplace()
    Dim FoundOne As Boolean

    Selection.HomeKey Unit:=wdStory, Extend:=wdMove
    FoundOne = True ' loop at least once

    Do While FoundOne ' loop until no date is found
        With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "([0-9]{1,2})[/]([0-9]{1,2})[/]([0-9]{4})"
            .Format = True
            .Forward = True
            .MatchWildcards = True
        End With

        Selection.Find.Execute Replace:=wdReplaceNone

        ' check the find to be sure it's a date
        If IsDate(Selection.Text) Then
            Selection.Text = Format(Selection.Text, "mmmm d, yyyy")
            Selection.Collapse wdCollapseEnd
        Else ' not a date - end loop
            FoundOne = False
        End If
    Loop
End Sub

Tip #3400 applies to Microsoft Word versions: 97 | 2000 | 2002 | 2003 | 2007


Ultimate Library! The most amazing collection ever offered in the history of WordTips places every tip—present and historical—at your fingertips. Check out the WordTips Ten-Year Library.

Helpful Links

Ask a Word Question
Make a Comment

WordTips
Word 2007 Tips
ExcelTips
Excel 2007 Tips
Vital News Home
 
WordTips FAQ
WordTips Premium
 
Learn Access Now
 
Beauty Tips
Car Care Tips
Cleaning Tips
College Tips
Cooking Tips
Gardening Tips
Health Tips
Home Improvement
Money Tips
Pet Tips
Tips.Net Home
 
Advertise on the
WordTips Site

 

Great Info!

Get tips like this every week in WordTips, a free productivity newsletter. Enter your e-mail address and click "Subscribe."
     
(Your e-mail address will never be shared with anyone, ever.)