在 Word 中切换日期选择器内容控件的区域设置

Toggle locale of Date Picker Content Control in Word

我正在尝试帮助某人制作一个模板以用于不同的语言:英国英语和西班牙语。

我有一个包含两个 DatePicker 内容控件的示例文档。在文档中,它们看起来像这样:

这是该文件的 temporary link

密码是:

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

根据Microsoft's Documentation,这是Read/Write属性。

如果日期没有设置值,代码可以工作,但如果已经有日期,它不会将英语日期转换为西班牙语日期,反之亦然。选择的新日期将使用正确的语言环境。

例如:

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