EugeneGardner
asked on
word VBA macro needs ordinal indicator
I would like to add the current date in the format such as 8th February 2009 to my Word 2007 template. I can get 8 February 2009 but have no idea how to get the ordinal indicators (st nd rd th) added.
Sub Autonew()
'
' Autonew Macro
'
'
ActiveDocument.Bookmarks("date").Select
Selection.InsertDateTime DateTimeFormat:="d MMMM yyyy", InsertAsField:=False
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry Chris, your function is totally equivalent... (°v°)
Thanks for that but as ever the beauty of the site is the number of permutations, and yours is a different approach so it's a question of what best meets the authors needs.
Chris
Chris
ASKER
That's great - thanks. Excuse the cheek of adding to the question at this late stage, but I would really like to superscript the ordinal indicator. I'm sure this must be possible by setting a marker, but can't find how that could be done. Any ideas ?
Adding 75 to the points.
Adding 75 to the points.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Chris. I have used harfang's code as I found it easier to follow. I tried incorporating what I thought was the superscript part of the last reply but I'm obviously missing something as nothing gets superscripted. Did I put the 'Selection.Font.Superscrip t = True' in the wrong place or is something else wrong ?
Option Explicit
Function MyDateFormat(pvarDate)
Dim strReturn As String
If Not IsDate(pvarDate) Then
MyDateFormat = Null
Exit Function
End If
Select Case Day(pvarDate)
Case 1, 21, 31: strReturn = "st"
Case 2, 22: strReturn = "nd"
Case 3, 23: strReturn = "rd"
Case Else: strReturn = "th"
End Select
Selection.TypeText (strReturn)
Selection.Font.Superscript = True
strReturn = Day(pvarDate) & strReturn & " " _
& Format(pvarDate, "mmmm yyyy")
MyDateFormat = strReturn
End Function
Sub Autonew()
Dim rngDate As Range
With ActiveDocument.Bookmarks
If .Exists("date") Then
Set rngDate = .Item("date").Range
rngDate.Text = MyDateFormat(Date)
.Add "date", rngDate
End If
End With
End Sub
Sorry to appear negative but I don't like the approach. As presented it seems to return some data direct into the document and some via the function. In my experience this part and part makes maintenance difficult. Also in the function it looks as though everything is being set to superscript not just the ordinal .. and I don't think on first sight that the returned string is being used by autonew.
That said I am happy to help as you want but maybay Harfang is best to help resolve your changes in his sub.
Chris
That said I am happy to help as you want but maybay Harfang is best to help resolve your changes in his sub.
Chris
FWIW, my code being hard to follow ... ouch!
It calculates the ordinal itself then outputs day, swirtches to superscripts outputs the ordinal then switches back to normal and outputs the month and year. I can't conceive any way of making it easier!
Chris
It calculates the ordinal itself then outputs day, swirtches to superscripts outputs the ordinal then switches back to normal and outputs the month and year. I can't conceive any way of making it easier!
Chris
ASKER
Sorry - no ouch intended, just that for my level of knowledge, I need it spelled out really simply even if efficiency is sacrificed as I am not a programmer.
:o)
I am generally not a proponent of efficiency in execution at the price of of understanding and whilst I suggested you seek Harfangs 'improvements' to your preferred form I am suspicious that by the time the switching between superscript and normal is said and done that it will be somewhat complicated rather than as I see it the step by step simplicity of the sub I posted.
I look forward to being proved wrong however as in the process I will no doubt learn something new.
Chris
I am generally not a proponent of efficiency in execution at the price of of understanding and whilst I suggested you seek Harfangs 'improvements' to your preferred form I am suspicious that by the time the switching between superscript and normal is said and done that it will be somewhat complicated rather than as I see it the step by step simplicity of the sub I posted.
I look forward to being proved wrong however as in the process I will no doubt learn something new.
Chris
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks both. FWIW I will use Harfang's solution as i find it easier to follow - the comments and spacing help.
That's fine as said it's up to you, but I assumed you wanted the ordinal in superscript not the whole data hence what you must have seen as complication in my code.
Chris
Chris