Search code examples
vbams-worddatepickerlocaleword-contentcontrol

Toggle locale of Date Picker Content Control in Word


I am trying to assist someone in producing a template to be used with different languages: EnglishUK and Spanish.

I have a sample document with two DatePicker Content Controls. In the document, they look like this:

screenshot

Here is a temporary link to that document.

The code is:

Sub DatePickerLocaleToggle()
    ' Charles Kenyon
    Dim cc As ContentControl
    For Each cc In ActiveDocument.ContentControls
        If cc.Type = wdContentControlDate Then
            If cc.DateDisplayLocale = wdEnglishUK Then
                cc.DateDisplayLocale = wdSpanish
            Else
                If cc.DateDisplayLocale = wdSpanish Then
                    cc.DateDisplayLocale = wdEnglishUK
                End If
            End If
        End If
    Next cc
End Sub

According to Microsoft's Documentation, this is a Read/Write property.

The code works if the date has no value set, but it does not convert the English Date to the Spanish Date or vice versa if there is a date already there. A new date picked will use the correct locale.


Solution

  • For example:

    Sub DatePickerLocaleToggle()
    Application.ScreenUpdating = False
    Dim CCtrl As ContentControl
    For Each CCtrl In ActiveDocument.ContentControls
      With CCtrl
        If .Type = wdContentControlDate Then
          Select Case .DateDisplayLocale
            Case wdEnglishUK
              If .ShowingPlaceholderText = True Then
                .DateDisplayLocale = wdSpanish
                .SetPlaceholderText Text:="Haga clic o toque para ingresar una fecha "
              Else
                .Range.Text = CCtrlDt(CCtrl, .DateDisplayFormat, wdSpanish)
              End If
            Case wdSpanish
              If .ShowingPlaceholderText = True Then
                .DateDisplayLocale = wdEnglishUK
                .SetPlaceholderText Text:="Click or tap to enter a date"
              Else
                .Range.Text = CCtrlDt(CCtrl, .DateDisplayFormat, wdEnglishUK)
              End If
          End Select
        End If
      End With
    Next
    Application.ScreenUpdating = True
    End Sub
    
    Function CCtrlDt(CCtrl As ContentControl, StrFMt As String, Lang As Long) As String
    Dim StrTmp As String, StrSplit As String, StrMnth As String, i As Long, Dt As Date
    With CCtrl
      For i = 0 To UBound(Split(StrFMt, " "))
        StrSplit = Split(StrFMt, " ")(i)
        If InStr(StrSplit, "ddd") = 0 Then
          If InStr(StrSplit, "M") = 1 Then
            StrMnth = Left(Split(.Range.Text, " ")(i), 3)
            If Lang = wdEnglishUK Then
              Select Case LCase(StrMnth)
                Case "ene": StrMnth = "Jan"
                Case "feb": StrMnth = "Feb"
                Case "mar": StrMnth = "Mar"
                Case "abr": StrMnth = "Apr"
                Case "may": StrMnth = "May"
                Case "jun": StrMnth = "Jun"
                Case "jul": StrMnth = "Jul"
                Case "ago": StrMnth = "Aug"
                Case "sep": StrMnth = "Sep"
                Case "oct": StrMnth = "Oct"
                Case "nov": StrMnth = "Nov"
                Case "dic": StrMnth = "Dec"
              End Select
            End If
            StrTmp = StrTmp & StrMnth & " "
          Else
            StrTmp = StrTmp & Split(.Range.Text, " ")(i) & " "
          End If
        End If
      Next
      Dt = CDate(Trim(StrTmp))
      .DateDisplayLocale = Lang
      CCtrlDt = Format(Dt, StrFMt)
    End With
    End Function