Solved

Word XP date format

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
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…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
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…

717 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