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
Solved

Word XP date format

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

809 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