问题描述
我正在尝试帮助某人制作可用于不同语言的模板:EnglishUK 和西班牙文。
我有一个包含两个 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,这是一个读/写属性。
如果日期没有设置值,则代码有效,但如果已存在日期,则不会将英文日期转换为西班牙日期,反之亦然。选择的新日期将使用正确的语言环境。
解决方法
例如:
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,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