?
Solved

Word XP date format

Posted on 2004-03-22
4
Medium Priority
?
533 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 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