获取当地时间会产生错误

时间:2017-05-16 19:32:31

标签: excel-vba object properties vba excel

以下代码返回错误Invalid qualifier

Set Nav_Selected = TL.ActiveBook.FindDataWatch("Navigator_SelectedOIDs")
Set Nav_Items = Nav_Selected.Data
Set block_path = Nav_Items.AttributeMap(0)
Set block_watch = TL.FindDataWatch(block_path)

Dim creationTime As Date
Dim localTime As String

'Get CreationTime
Set myWF = block_watch.Data
creationTime = myWF.Properties.Item("Created")
Set localTime = creationTime.ToLocalTime

我试图从creationTime对象获取localTime属性,只是不知道如何做到这一点。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

要解决此问题,我在描述GMT和DLST更正的this article后进行了更改:

首先,我添加了这些函数来将字符串解析为日期对象,然后调整日光节省时间(DLST)和格林威治时间中位数(GMT)

Function GetLocalTimeFromGMT(Optional GMTTime As Date) As Date
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GetLocalTimeFromGMT
' This returns the Local Time from a GMT time. If GMTTime is present and
' greater than 0, it is assumed to be the GMT from which we will calculate
' Local Time. If GMTTime is 0 or omitted, it is assumed to be the GMT
' time.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim GMT As Date
Dim TZI As TIME_ZONE_INFORMATION
Dim DST As TIME_ZONE
Dim localTime As Date

DST = GetTimeZoneInformation(TZI)
localTime = GMTTime - TimeSerial(0, TZI.Bias, 0) + IIf(DST = 
TIME_ZONE_DAYLIGHT, TimeSerial(1, 0, 0), 0)
GetLocalTimeFromGMT = localTime

End Function
----------------------------------------------------------------------------
Function ParseDateTime(dt As String) As Date
  ParseDateTime = CDate(dt)
End Function

接下来,我修改了main函数来调用时间调整函数:

'Get CreationTime
Set myWF = block_watch.Data
creationTime = myWF.Properties.Item("Created")
localTime = ParseDateTime(creationTime)

我现在有时间投入到我的功能中,可以随时修正方法并在几秒钟内准确。