Solved

Word XP date format

Posted on 2004-03-22
4
521 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When creating Microsoft Word-based forms there may be a need to have a form field repeated throughout the whole document. For instance, with a company name, you may want this information repeated automatically throughout the document rather than man…
Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

813 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

11 Experts available now in Live!

Get 1:1 Help Now