Solved

Word XP date format

Posted on 2004-03-22
4
517 Views
Last Modified: 2008-02-01
How do I get Word's Insert Date function to automatically "humanise" the format to add superscripted "st", "nd", "rd", "th" as appropriate to the day?
0
Comment
Question by:genders
  • 2
4 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 10655915
This works in Word 97

Create a macro in your template with the specific name "InsertDateTime"

Sub InsertDateTime()
Dim InsertPoint As Long
Dim rn As Range
Dim strDate As String
Dim strDay As String
Dim strSuffix As String

Set rn = Selection.Range
rn.Collapse wdCollapseEnd
With Dialogs(wdDialogInsertDateTime)
    .Show
rn.End = Selection.Range.End
strDate = rn.Text
Select Case .datetimepic
    Case "dd MMMM yyyy" ' you may choose to add more formats here
        strDay = Left$(strDate, 2)
        Select Case Val(strDay)
            Case 1, 21, 31
                strSuffix = "st"
            Case 2, 22
                strSuffix = "nd"
            Case 3, 23
                strSuffix = "rd"
            Case Else
                strSuffix = "th"
        End Select
        strDate = strDay & strSuffix & Mid$(strDate, 3)
        rn.Text = strDate
    End Select
End With
End Sub

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 10655946
Sorry, forgot the superscripting.

Sub InsertDateTime()
    Dim InsertPoint As Long
    Dim rn As Range
    Dim strDate As String
    Dim strDay As String
    Dim strSuffix As String
   
    Set rn = Selection.Range
    rn.Collapse wdCollapseEnd
    With Dialogs(wdDialogInsertDateTime)
        .Show
        rn.End = Selection.Range.End
        strDate = rn.Text
       
        Select Case .datetimepic
            Case "dd MMMM yyyy" ' you may choose to add more formats here
                strDay = Left$(strDate, 2)
                Select Case Val(strDay)
                    Case 1, 21, 31
                        strSuffix = "st"
                    Case 2, 22
                        strSuffix = "nd"
                    Case 3, 23
                        strSuffix = "rd"
                    Case Else
                        strSuffix = "th"
                End Select
                strDate = strDay & strSuffix & Mid$(strDate, 3)
                rn.Text = strDate
                rn.Start = rn.Start + 2
                rn.End = rn.Start + 2
                rn.Font.Superscript = True
        End Select
    End With
End Sub




0
 
LVL 24

Accepted Solution

by:
R_Rajesh earned 500 total points
ID: 10658166
genders,

After pasting the code below, try inserting dates as usual (insert - date and time). each time a date is inserted the code will *humanise* it. The code will ignore certain date formats (for example 2004-03-23)

Sub InsertDateTime()
Dim fld As Field, fld1 As Field, ret As Long, c, i
ret = Dialogs(wdDialogInsertDateTime).Show
If ret Then
Set fld = ActiveDocument.Fields(ActiveDocument.Range(0, Selection.End).Fields.Count)
For Each c In fld.Result.Characters
If c = " " Then i = i + 1
Next c
If i > 1 And InStr(1, fld.Result.Text, ":") = 0 Then
Set fld1 = Selection.Fields.Add(Range:=Selection.Range, Text:=" CREATEDATE \@ ""d"" \*ordinal")
fld.Result.Text = Replace(fld.Result, Day(Date), fld1.Result)
fld.Select: Selection.Find.ClearFormatting
Selection.Find.Execute FindText:=Right(fld1.Result, 2): Selection.Font.Superscript = True
fld1.Select: fld1.Delete
End If
End If
End Sub


Rajesh
0
 

Author Comment

by:genders
ID: 10682792
Thanks to GrahamSkan for attempting this, however I felt Rajesh's solution was more elegant.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now