【问题标题】:Toggle locale of Date Picker Content Control in Word在 Word 中切换日期选择器内容控件的区域设置
【发布时间】:2021-06-20 19:11:10
【问题描述】:

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

我有一个包含两个 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,这是一个读/写属性。

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

【问题讨论】:

    标签: vba ms-word datepicker locale word-contentcontrol


    【解决方案1】:

    例如:

    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
    

    【讨论】:

    • 我将您的宏和函数以及各种内容控件添加到示例文档中,以防有人想看到它的实际效果。 dropbox.com/s/vf7rmo2t1di79gt/…
    • 我对代码做了一些小的修改以简化它。
    • 谢谢。我已经用您的修订更新了示例/测试文档。
    猜你喜欢
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多