#1
|
|||
|
|||
Ordinal Numbers with DatePicker
Forgive me if I'm not using the correct terminology. I've created a date picker content control which displays a calendar for picking dates. I use the following format display: d of MMMM, yyyy
But I would like the day to be an ordinal number. What do I have to do to achieve that? Thanks |
#2
|
||||
|
||||
Unfortunately date content controls do not cater for ordinal dates, however, you can use the control to select the date, then using a macro change the control type to Rich Text and format the selected date as required. In the thisdocument module of the document or its template add the following. Then when you leave the field it will be converted to display the field as required. The three lines that add the superscript are optional.
Click into the changed field to select a new date. Code:
Option Explicit 'Graham Mayor - https://www.gmayor.com - Last updated - 20 Sep 2023 Private Const sList As String = "0123456789" Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl) Dim oRng As Range Dim dDate As Date Select Case ContentControl.Title Case Is = "Date" 'The title of the date field If ContentControl.ShowingPlaceholderText = False Then If ContentControl.Type = wdContentControlRichText Then Set oRng = ContentControl.Range oRng.MoveStartWhile sList oRng.End = oRng.Start + 5 oRng.Text = "" Set oRng = ContentControl.Range dDate = oRng.Text ContentControl.Type = wdContentControlDate oRng.Text = Format(dDate, "dd/MM/yyyy") End If End If End Select End Sub Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) Dim oRng As Range Dim dDate As Date Select Case ContentControl.Title Case Is = "Date" 'The title of the date field If ContentControl.ShowingPlaceholderText = False Then If ContentControl.Type = wdContentControlDate Then Set oRng = ContentControl.Range dDate = CDate(oRng.Text) ContentControl.Type = wdContentControlRichText oRng.Text = Format(dDate, "d") & DateOrdinal(Format((dDate), "d")) & _ " of " & Format((dDate), "MMMM yyyy") oRng.MoveStartWhile sList oRng.End = oRng.Start + 2 oRng.Font.Superscript = True End If End If End Select End Sub Private Function DateOrdinal(Val As Long) As String 'Paul Edstein Dim strOrd As String If (Val Mod 100) < 11 Or (Val Mod 100) > 13 Then strOrd = Choose(Val Mod 10, "st", "nd", "rd") & "" DateOrdinal = IIf(strOrd = "", "th", strOrd) lbl_Exit: Exit Function End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Thank you very much
|
#4
|
|||
|
|||
I'm not an expert on vba and macros. Do I past the code as a module? How do I run it? And should the code dDate, "dd/MM/yyyy"be changed to my date format?
Thanks again, Roger |
#5
|
||||
|
||||
The code as posted goes in the ThisDocument module of the document, which should be saved as macro enabled. It runs automatically when you enter and leave the content control.
I will probably create a web page with a fuller explanation, but I am currently away from my office until at least December.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#6
|
|||
|
|||
Okay, Thank you, What are the three lines that make it superscript? Also, your line
" oRng.Text = Format(dDate, "dd/MM/yyyy") Does that have to match the format of the date date picker? I use "dd of MM, yyy" Does the macro have to be written that way? |
#7
|
|||
|
|||
I think I did what you said, but it doesn't work
|
#8
|
|||
|
|||
You appear to be having difficulty understanding the term "OnExit". This refers to the insertion point leaving the content control. When the insertion point is moved from away from the content control the Document_ContentControlOnExit event code is triggered which then formats the date. See attached images.
|
#9
|
|||
|
|||
I'm sorry. I'm much less skilled then you ARE. I'm not getting what you're saying
|
#10
|
||||
|
||||
If you need help to do this, post a sample document so someone can see what you have actually done with the code and what elements of the document align with what the code is expecting to see.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#11
|
|||
|
|||
See attached example document
|
#12
|
||||
|
||||
I have modified the code to reflect the fact that you have two date fields, both now titled independently from one another. Select a date then click outside the content control.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#13
|
|||
|
|||
You're responding to the wrong person. I posted that file in an effort to help the OP understand how to use your code.
|
#14
|
|||
|
|||
Different Version
Here is a different version that doesn't absolutely require the exit event.
|
#15
|
|||
|
|||
I'm not sure how providing a more complex solution without any explanation of how it is implemented will be helpful to the OP who, from the comments they have made, seems to already be struggling to understand the solution provided by gmayor.
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Replace all Images with ordinal numbers in multiple files | beginner | Word | 0 | 09-19-2021 02:19 AM |
Formatting th at the end of ordinal dates | DBlomgren | Publisher | 0 | 01-23-2016 09:44 AM |
How do I code ListBox and DatePicker in UserForm? - Word 2013 | ickelly | Word VBA | 4 | 08-05-2015 04:07 PM |
Datepicker format | piper7971 | Excel | 1 | 07-26-2015 02:29 AM |
MergeField Alphabetic Ordinal substitutes _ for Z | khinnenkamp | Mail Merge | 1 | 09-29-2013 07:01 PM |