[英]I want to get the value of two Datepicker on VBA ms-Word and Calculate the difference with DateDiff
I want to get the value of two Datepicker on VBA ms-Word and then use DateDiff to calculate the difference between two dates and make it display on the document.我想在 VBA ms-Word 上获取两个 Datepicker 的值,然后使用 DateDiff 计算两个日期之间的差异并使其显示在文档上。 I can get the value of the Datepicker by using "ContentControl.Range.Text" but when I use it with DateDiff is saying "Type missmatch".我可以通过使用“ContentControl.Range.Text”来获取 Datepicker 的值,但是当我将它与 DateDiff 一起使用时会说“类型不匹配”。
Sub Test1()
'
' Test1 Macro
'
'
Dim doc As Document
Set doc = Application.ActiveDocument
Dim CCtrl As String
CCtrl = "Date1"
Dim chkExt As Boolean
chkExt = False
Dim control As ContentControl
For Each control In doc.ContentControls
If control.Tag = CCtrl Then
chkExt = True
Exit For
End If
Next
If chkExt = False Then
MsgBox "Nothing"
Exit Sub
End If
Dim ChangeType As Date
ChangeType = control.Range.Text
Debug.Print IsDate(ChangeType)
End Sub
UPDATE:I Find the problem The display of the date is affecting the code if the date is display this way is working fine Normal Format更新:我发现问题如果日期以这种方式显示正常,则日期显示会影响代码正常格式
but if it display in Thai format is not working Thai Format但如果它以泰语格式显示,则泰语格式不起作用
so is there any workaround to make the display as Thai format but the value of it is the same as the normal one.那么是否有任何解决方法可以使显示为泰语格式,但它的值与正常值相同。
I found the Solution Thanks to my Friend so the way to do it is by split the date and then use DateDiff this way on the Document the date still show as thai format but still got the value to use in DateDiff Here the Code that work for me:感谢我的朋友,我找到了解决方案,所以方法是拆分日期,然后在文档上以这种方式使用 DateDiff 日期仍然显示为泰语格式,但仍然获得了在 DateDiff 中使用的值 Here the Code that work for我:
Sub Test1()
'
' Test1 Macro
'
'
Dim doc As Document
Set doc = Application.ActiveDocument
Dim CCtrl, CCtrl2 As String
CCtrl = "Date1"
CCtrl2 = "Date2"
Dim chkExt As Boolean
chkExt = False
Dim control As ContentControl
Dim control2 As ContentControl
For Each control2 In doc.ContentControls
If control2.Tag = CCtrl2 Then
control2.DateDisplayFormat = "d ´´´´ bbbb"
Dim Result1() As String
Result1() = Split(control2.Range.Text)
Dim MyClasses1 As New Collection
MyClasses1.Add Item:="01", Key:="Á¡ÃÒ¤Á"
MyClasses1.Add Item:="02", Key:="¡ØÁÀҾѹ"
MyClasses1.Add Item:="03", Key:="ÁÕ¹Ò¤Á"
MyClasses1.Add Item:="04", Key:="àÁÉÒ¹"
MyClasses1.Add Item:="05", Key:="¾ÄÉÀÒ¤Á"
MyClasses1.Add Item:="06", Key:="ÁԶعÒ¹"
MyClasses1.Add Item:="07", Key:="¡Ã¡®Ò¤Á"
MyClasses1.Add Item:="08", Key:="ÊÔ§ËÒ¤Á"
MyClasses1.Add Item:="09", Key:="¡Ñ¹ÂÒ¹"
MyClasses1.Add Item:="10", Key:="µØÅÒ¤Á"
MyClasses1.Add Item:="11", Key:="¾ÄȨԡÒ¹"
MyClasses1.Add Item:="12", Key:="¸Ñ¹ÇÒ¤Á"
Dim engDate1 As String
engDate1 = Result1(0) & "/" & MyClasses1(Result1(1)) & "/" & Result1(2)
Debug.Print IsDate(engDate1)
Exit For
End If
Next
For Each control In doc.ContentControls
'check if there is a tag
If control.Tag = CCtrl Then
control.DateDisplayFormat = "d ´´´´ bbbb"
'For spliting Date as 00 00 00
Dim Result() As String
Result() = Split(control.Range.Text)
'Creating Keys
Dim MyClasses As New Collection
MyClasses.Add Item:="01", Key:="Á¡ÃÒ¤Á"
MyClasses.Add Item:="02", Key:="¡ØÁÀҾѹ"
MyClasses.Add Item:="03", Key:="ÁÕ¹Ò¤Á"
MyClasses.Add Item:="04", Key:="àÁÉÒ¹"
MyClasses.Add Item:="05", Key:="¾ÄÉÀÒ¤Á"
MyClasses.Add Item:="06", Key:="ÁԶعÒ¹"
MyClasses.Add Item:="07", Key:="¡Ã¡®Ò¤Á"
MyClasses.Add Item:="08", Key:="ÊÔ§ËÒ¤Á"
MyClasses.Add Item:="09", Key:="¡Ñ¹ÂÒ¹"
MyClasses.Add Item:="10", Key:="µØÅÒ¤Á"
MyClasses.Add Item:="11", Key:="¾ÄȨԡÒ¹"
MyClasses.Add Item:="12", Key:="¸Ñ¹ÇÒ¤Á"
'Put together the Date
Dim engDate As String
engDate = Result(0) & "/" & MyClasses(Result(1)) & "/" & Result(2)
Debug.Print IsDate(engDate)
Exit For
End If
Next
Dim ccdisplayDateDiff As ContentControl
Dim displayDateDiff As String
displayDateDiff = "displayDateDiff"
For Each ccdisplayDateDiff In doc.ContentControls
If ccdisplayDateDiff.Tag = displayDateDiff Then
ccdisplayDateDiff.Range.Text = DateDiff("d", engDate, engDate1)
Exit For
End If
Next
End Sub
ps.附言。 The Alien language is Thai language that can't be display and in the collection is the month in Thai language.Sorry for my bad English.外星人的语言是泰语,无法显示,收藏中是泰语的月份。抱歉我的英语不好。
Thanks everyone for helping:)感谢大家的帮助:)
Sub Test1()
'
' Test1 Macro
'
'
Dim doc As Document
Set doc = Application.ActiveDocument
Dim CCtrl, CCtrl2 As String
CCtrl = "Date1"
CCtrl2 = "Date2"
Dim chkExt As Boolean
chkExt = False
Dim control As ContentControl
Dim control2 As ContentControl
For Each control2 In doc.ContentControls
If control2.Tag = CCtrl2 Then
control2.DateDisplayFormat = "d ´´´´ bbbb"
Dim Result1() As String
Result1() = Split(control2.Range.Text)
Dim MyClasses1 As New Collection
MyClasses1.Add Item:="01", Key:="Á¡ÃÒ¤Á"
MyClasses1.Add Item:="02", Key:="¡ØÁÀҾѹ"
MyClasses1.Add Item:="03", Key:="ÁÕ¹Ò¤Á"
MyClasses1.Add Item:="04", Key:="àÁÉÒ¹"
MyClasses1.Add Item:="05", Key:="¾ÄÉÀÒ¤Á"
MyClasses1.Add Item:="06", Key:="ÁԶعÒ¹"
MyClasses1.Add Item:="07", Key:="¡Ã¡®Ò¤Á"
MyClasses1.Add Item:="08", Key:="ÊÔ§ËÒ¤Á"
MyClasses1.Add Item:="09", Key:="¡Ñ¹ÂÒ¹"
MyClasses1.Add Item:="10", Key:="µØÅÒ¤Á"
MyClasses1.Add Item:="11", Key:="¾ÄȨԡÒ¹"
MyClasses1.Add Item:="12", Key:="¸Ñ¹ÇÒ¤Á"
Dim engDate1 As String
engDate1 = Result1(0) & "/" & MyClasses1(Result1(1)) & "/" & Result1(2)
Debug.Print IsDate(engDate1)
Exit For
End If
Next
For Each control In doc.ContentControls
'check if there is a tag
If control.Tag = CCtrl Then
control.DateDisplayFormat = "d ´´´´ bbbb"
'For spliting Date as 00 00 00
Dim Result() As String
Result() = Split(control.Range.Text)
'Creating Keys
Dim MyClasses As New Collection
MyClasses.Add Item:="01", Key:="Á¡ÃÒ¤Á"
MyClasses.Add Item:="02", Key:="¡ØÁÀҾѹ"
MyClasses.Add Item:="03", Key:="ÁÕ¹Ò¤Á"
MyClasses.Add Item:="04", Key:="àÁÉÒ¹"
MyClasses.Add Item:="05", Key:="¾ÄÉÀÒ¤Á"
MyClasses.Add Item:="06", Key:="ÁԶعÒ¹"
MyClasses.Add Item:="07", Key:="¡Ã¡®Ò¤Á"
MyClasses.Add Item:="08", Key:="ÊÔ§ËÒ¤Á"
MyClasses.Add Item:="09", Key:="¡Ñ¹ÂÒ¹"
MyClasses.Add Item:="10", Key:="µØÅÒ¤Á"
MyClasses.Add Item:="11", Key:="¾ÄȨԡÒ¹"
MyClasses.Add Item:="12", Key:="¸Ñ¹ÇÒ¤Á"
'Put together the Date
Dim engDate As String
engDate = Result(0) & "/" & MyClasses(Result(1)) & "/" & Result(2)
Debug.Print IsDate(engDate)
Exit For
End If
Next
Dim ccdisplayDateDiff As ContentControl
Dim displayDateDiff As String
displayDateDiff = "displayDateDiff"
For Each ccdisplayDateDiff In doc.ContentControls
If ccdisplayDateDiff.Tag = displayDateDiff Then
ccdisplayDateDiff.Range.Text = DateDiff("d", engDate, engDate1)
Exit For
End If
Next
End Sub
You can achieve this simply by changing the date locale and format to English, collecting your data, then change it back to Thai.您只需将日期区域设置和格式更改为英语,收集您的数据,然后将其更改回泰语即可实现此目的。
Something like this:是这样的:
Sub Test2()
Dim doc As Document
Set doc = Application.ActiveDocument
Const CCtrl As String = "Date1"
Const CCtrl2 As String = "Date2"
Const displayDateDiff As String = "displayDateDiff"
Dim control As ContentControl
Dim ccdisplayDateDiff As ContentControl
Dim engDate As String, engDate1 As String
For Each control In doc.ContentControls
Select Case control.Tag
Case CCtrl, CCtrl2
control.DateDisplayLocale = wdEnglishUS
control.DateDisplayFormat = "MMMM d YYYY"
If control.Tag = CCtrl Then
engDate = control.Range.Text
ElseIf control.Tag = CCtrl2 Then
engDate1 = control.Range.Text
End If
control.DateDisplayLocale = wdThai
control.DateDisplayFormat = "d ´´´´ bbbb"
Case displayDateDiff
Set ccdisplayDateDiff = control
End Select
Next
If Not ccdisplayDateDiff Is Nothing Then ccdisplayDateDiff.Range.Text = DateDiff("d", engDate, engDate1)
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.