locked
Date Picker format to include a suffix after the day ie. 1st January 2011 RRS feed

  • Question

  • Using Microsoft Word 2010 I have inserted a Date Picker Content Control however wish to change the format to include a suffix after the day ie. 1st January 2011 etc.  Any ideas on the format I should be using.  I thought it was dS MMMM yyyy.  This just inserts a number though.  any ideas would be greatly appreciated - thank you!
    Friday, December 9, 2011 1:29 PM

Answers

  • Here's a macro that both adds the ordinal suffix and superscripts it:

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    Dim i As Long, j As String, Rng As Range
    With ContentControl
      If .Type <> wdContentControlDate Then Exit Sub
      .Type = wdContentControlRichText
      Set Rng = .Range
      If .Title = "MyDate" Then
        For i = 0 To UBound(Split(.Range.Text, " "))
          j = Split(.Range.Text, " ")(i)
          If IsNumeric(j) Then
              With Rng
                .Start = .Start + InStr(ContentControl.Range.Text, j) + Len(j) - 1
                .End = .Start
                .InsertAfter Ordinal(Val(j))
                .Font.Superscript = True
              End With
              .Type = wdContentControlDate
            Exit For
          End If
        Next
      End If
    End With
    End Sub

    Function Ordinal(Val As Integer) As String
    Dim strOrd As String
    If (Val Mod 100) < 11 Or (Val Mod 100) > 13 Then strOrd = Choose(Val Mod 10, "st", "nd", "rd") & ""
    Ordinal = IIf(strOrd = "", "th", strOrd)
    End Function

    With the above, it's assumed the Content Control is titled 'Mydate'. You add the code to the document's 'This Document' module.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Saturday, December 10, 2011 8:40 AM
  • U can replace control on th text, but that will by doog if user selected control once.

    You should to prepare template dotx

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    If ContentControl.Type <> wdContentControlDate Then Exit Sub
    If ContentControl.Range.Text = ContentControl.PlaceholderText Then Exit Sub
    Dim rng As Range: Set rng = ContentControl.Range
    Dim data$: data = ContentControl.Range.Text
    ContentControl.Range.Delete
    ContentControl.Delete
    rng.Text = SetDateFormat(CDate(data))
    End Sub
    
    'and function SetDateFormat is needed
    

    save document and test it.


    Oskar Shon, Office System MVP
    • Proposed as answer by VBAToolsMVP Friday, December 9, 2011 2:53 PM
    • Marked as answer by William Zhou CHN Friday, December 16, 2011 8:49 AM
    Friday, December 9, 2011 2:53 PM

All replies

  • I supose that U must use Form 'n' VBACode to do this.

    That function will be helpfull

    Function SetDateFormat(d As Date)
    Select Case Day(d)
        Case 1:    SetDateFormat = Format(d, "d""st ""mmmm yyyy")
        Case 2:    SetDateFormat = Format(d, "d""nd ""mmmm yyyy")
        Case Else: SetDateFormat = Format(d, "d""th ""mmmm yyyy")
    End Select
    End Function
    


     

     


    Oskar Shon, Office System MVP
    Friday, December 9, 2011 2:46 PM
  • U can replace control on th text, but that will by doog if user selected control once.

    You should to prepare template dotx

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    If ContentControl.Type <> wdContentControlDate Then Exit Sub
    If ContentControl.Range.Text = ContentControl.PlaceholderText Then Exit Sub
    Dim rng As Range: Set rng = ContentControl.Range
    Dim data$: data = ContentControl.Range.Text
    ContentControl.Range.Delete
    ContentControl.Delete
    rng.Text = SetDateFormat(CDate(data))
    End Sub
    
    'and function SetDateFormat is needed
    

    save document and test it.


    Oskar Shon, Office System MVP
    • Proposed as answer by VBAToolsMVP Friday, December 9, 2011 2:53 PM
    • Marked as answer by William Zhou CHN Friday, December 16, 2011 8:49 AM
    Friday, December 9, 2011 2:53 PM
  • Thanks for your reply!

    I'm using the Quick Parts within Word's ribbon and have insert the Publish Date.  I have the Developer tab added so I can view the properties of this Date field. Within these properties it allows you to set how you want the date to display with numerous standard formats.  However you can edit it to a custom format.  So I'm not actually using VBA.  

    Or did you mean there is not another way other that to write a method yourself for it?  Thanks again!

    Friday, December 9, 2011 5:38 PM
  • "I'm not actually using VBA."

    This is the best reason to start. ;]

    Prepare Your document as well and press [Ctrl+F11]\menu\instert\module add code and save as .dotm

    or if user of this document that's only U so add control from Quick Parts and remove it by code (to get your's specyfic format).


    Oskar Shon, Office System MVP

    Press if Helpful

    Friday, December 9, 2011 6:40 PM
  • Here's a macro that both adds the ordinal suffix and superscripts it:

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    Dim i As Long, j As String, Rng As Range
    With ContentControl
      If .Type <> wdContentControlDate Then Exit Sub
      .Type = wdContentControlRichText
      Set Rng = .Range
      If .Title = "MyDate" Then
        For i = 0 To UBound(Split(.Range.Text, " "))
          j = Split(.Range.Text, " ")(i)
          If IsNumeric(j) Then
              With Rng
                .Start = .Start + InStr(ContentControl.Range.Text, j) + Len(j) - 1
                .End = .Start
                .InsertAfter Ordinal(Val(j))
                .Font.Superscript = True
              End With
              .Type = wdContentControlDate
            Exit For
          End If
        Next
      End If
    End With
    End Sub

    Function Ordinal(Val As Integer) As String
    Dim strOrd As String
    If (Val Mod 100) < 11 Or (Val Mod 100) > 13 Then strOrd = Choose(Val Mod 10, "st", "nd", "rd") & ""
    Ordinal = IIf(strOrd = "", "th", strOrd)
    End Function

    With the above, it's assumed the Content Control is titled 'Mydate'. You add the code to the document's 'This Document' module.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Saturday, December 10, 2011 8:40 AM
  • That's great thank you both for your help!! Much appreciated! :-)
    Monday, December 12, 2011 10:00 AM
  • Please help!

    This is the solution that I am looking for, however, I am struggling with VBA!!

    Would you be so kind as to give me a step-by-step guide. I'm not daft and I've coded before but I'm ready to throw the damn computer out the window! lol

    Sunday, March 10, 2019 8:49 PM
  • For PC macro installation & usage instructions, see: http://www.gmayor.com/installing_macro.htm
    For Mac macro installation & usage instructions, see: https://wordmvp.com/Mac/InstallMacro.html


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, March 10, 2019 9:43 PM