Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-19-2023, 01:44 PM
roger.stern@mail.com roger.stern@mail.com is offline Ordinal Numbers with DatePicker Windows 10 Ordinal Numbers with DatePicker Office 2021
Novice
Ordinal Numbers with DatePicker
 
Join Date: Sep 2023
Posts: 13
roger.stern@mail.com is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 09-19-2023, 11:20 PM
gmayor's Avatar
gmayor gmayor is offline Ordinal Numbers with DatePicker Windows 10 Ordinal Numbers with DatePicker Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 09-27-2023, 09:17 AM
roger.stern@mail.com roger.stern@mail.com is offline Ordinal Numbers with DatePicker Windows 10 Ordinal Numbers with DatePicker Office 2021
Novice
Ordinal Numbers with DatePicker
 
Join Date: Sep 2023
Posts: 13
roger.stern@mail.com is on a distinguished road
Default

Thank you very much
Reply With Quote
  #4  
Old 09-27-2023, 09:19 AM
roger.stern@mail.com roger.stern@mail.com is offline Ordinal Numbers with DatePicker Windows 10 Ordinal Numbers with DatePicker Office 2021
Novice
Ordinal Numbers with DatePicker
 
Join Date: Sep 2023
Posts: 13
roger.stern@mail.com is on a distinguished road
Default

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
Reply With Quote
  #5  
Old 09-27-2023, 11:47 PM
gmayor's Avatar
gmayor gmayor is offline Ordinal Numbers with DatePicker Windows 10 Ordinal Numbers with DatePicker Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #6  
Old 10-23-2023, 11:20 AM
roger.stern@mail.com roger.stern@mail.com is offline Ordinal Numbers with DatePicker Windows 10 Ordinal Numbers with DatePicker Office 2021
Novice
Ordinal Numbers with DatePicker
 
Join Date: Sep 2023
Posts: 13
roger.stern@mail.com is on a distinguished road
Default

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?
Reply With Quote
  #7  
Old 10-23-2023, 11:38 AM
roger.stern@mail.com roger.stern@mail.com is offline Ordinal Numbers with DatePicker Windows 10 Ordinal Numbers with DatePicker Office 2021
Novice
Ordinal Numbers with DatePicker
 
Join Date: Sep 2023
Posts: 13
roger.stern@mail.com is on a distinguished road
Default

I think I did what you said, but it doesn't work
Reply With Quote
  #8  
Old 10-23-2023, 12:19 PM
Italophile Italophile is offline Ordinal Numbers with DatePicker Windows 11 Ordinal Numbers with DatePicker Office 2021
Expert
 
Join Date: Mar 2022
Posts: 333
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Quote:
Originally Posted by roger.stern@mail.com View Post
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.
Attached Images
File Type: png Screenshot 2023-10-23 201344.png (2.2 KB, 24 views)
File Type: png Screenshot 2023-10-23 201432.png (10.4 KB, 23 views)
File Type: png Screenshot 2023-10-23 201453.png (1.9 KB, 23 views)
File Type: png Screenshot 2023-10-23 201522.png (2.2 KB, 23 views)
Reply With Quote
  #9  
Old 10-25-2023, 02:28 PM
roger.stern@mail.com roger.stern@mail.com is offline Ordinal Numbers with DatePicker Windows 10 Ordinal Numbers with DatePicker Office 2021
Novice
Ordinal Numbers with DatePicker
 
Join Date: Sep 2023
Posts: 13
roger.stern@mail.com is on a distinguished road
Default

I'm sorry. I'm much less skilled then you ARE. I'm not getting what you're saying
Reply With Quote
  #10  
Old 10-25-2023, 03:36 PM
Guessed's Avatar
Guessed Guessed is offline Ordinal Numbers with DatePicker Windows 10 Ordinal Numbers with DatePicker Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #11  
Old 10-26-2023, 12:30 AM
Italophile Italophile is offline Ordinal Numbers with DatePicker Windows 11 Ordinal Numbers with DatePicker Office 2021
Expert
 
Join Date: Mar 2022
Posts: 333
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

See attached example document
Attached Files
File Type: docm Ordinal dates example.docm (35.4 KB, 3 views)
Reply With Quote
  #12  
Old 10-28-2023, 12:48 AM
gmayor's Avatar
gmayor gmayor is offline Ordinal Numbers with DatePicker Windows 10 Ordinal Numbers with DatePicker Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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.
Attached Files
File Type: docm Ordinal dates example.docm (38.3 KB, 8 views)
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #13  
Old 10-28-2023, 02:37 AM
Italophile Italophile is offline Ordinal Numbers with DatePicker Windows 11 Ordinal Numbers with DatePicker Office 2021
Expert
 
Join Date: Mar 2022
Posts: 333
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Quote:
Originally Posted by gmayor View Post
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.
You're responding to the wrong person. I posted that file in an effort to help the OP understand how to use your code.
Reply With Quote
  #14  
Old 10-28-2023, 05:56 AM
gmaxey gmaxey is offline Ordinal Numbers with DatePicker Windows 10 Ordinal Numbers with DatePicker Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default Different Version

Here is a different version that doesn't absolutely require the exit event.
Attached Files
File Type: docm Insert Ordinal Date Ver 1.5.docm (60.2 KB, 6 views)
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #15  
Old 10-28-2023, 08:51 AM
Italophile Italophile is offline Ordinal Numbers with DatePicker Windows 11 Ordinal Numbers with DatePicker Office 2021
Expert
 
Join Date: Mar 2022
Posts: 333
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Quote:
Originally Posted by gmaxey View Post
Here is a different version that doesn't absolutely require the exit event.
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.
Reply With Quote
Reply

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
Ordinal Numbers with DatePicker 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

Other Forums: Access Forums

All times are GMT -7. The time now is 06:43 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft