oNode设置为Nothing,但为什么,以及如何解决?

时间:2014-09-11 14:41:11

标签: date datetime xpath vbscript msxml

我正在尝试开发一个令牌(在这种情况下是一段在更大的VBScript中运行的代码),它将第三方软件提供的XML信息返回到word插件,使用书签提供参数令牌。

所以这是正在发生的事情,

XmlDoc.SetProperty "SelectionLanguage", "XPath"

ReturnData = vbNullString

Public Function GetParameterXml()
    GetParameterXml = _
    "<Parameters>" & _
        "<Parameter Value='Last_Hearing' Code='L' Description='Last_Hearing' Type='Combo'>" & _
            "<Options>" & _
                "<Option Code='' Description='True' Value='True' />" & _
                "<Option Code='' Description='False' Value='False' />" & _
            "</Options>" & _
        "</Parameter>" & _
    "</Parameters>"     
End Function

Dim oNode : Set oNode = XmlDoc.SelectSingleNode("/Record/CelloXml/Integration/Case/Hearing/Setting/CourtroomMinutes/Comment")
Dim lastHearing : Set lastHearing = Parameters.Item( BookMark, "Last_Hearing" )     

If IsNull(lastHearing) Then
    lastHearing = False
End If
stop
If lastHearing.Value = "True" Then
    Dim dateNodes : Set dateNodes = XmlDoc.SelectNodes("/Record/CelloXml/Integration/Case/Hearing/Setting/HearingDate")
    Dim mostRecentHearingDate
    Dim dateNode
    Dim todaysDate
    todaysDate = Date

    Dim dateList : Set dateList = CreateObject("System.Collections.ArrayList")
    For Each dateNode In dateNodes
        dateList.Add CDate(dateNode.Text)
    Next
    dateList.Sort()

    Dim tempDate
    For Each tempDate In dateList
        If tempDate < todaysDate Then
            mostRecentHearingDate = tempDate
        End If
    Next
    mostRecentHearingDate = CStr(mostRecentHearingDate)
    Set oNode = XmlDoc.selectSingleNode("/Record/CelloXml/Integration/Case/Hearing/Setting[HearingDate/text()='" & mostRecentHearingDate & "']/CourtroomMinutes/Comment")
End If

If Not oNode Is Nothing Then
    ReturnData = oNode.text
Else
    ReturnData = vbNullString
End If

一切都按照我想要的方式进行,直到

Set oNode = XmlDoc.selectSingleNode("/Record/CelloXml/Integration/Case/Hearing/Setting[HearingDate/text()='" & mostRecentHearingDate & "']/CourtroomMinutes/Comment")

我需要dateList来保存日期(或日期文字),因为我假设如果我尝试将日期排序为字符串而不是实际日期,我会得到一个错误的排序,所以我转换了文本日期(或日期文字)的节点,并将其添加到dateList

当我完成所有计算后,我需要一个字符串在我的XPath中运行,如果我将日期(作为字符串{08/05/2014})硬编码到它运行的XPath查询中,但是当我使用mostRecentHearingDateCStr转换为字符串,然后将oNode设置为Nothing

节点存在并保存数据

所以,

  • 为什么会这样?
  • 如何让它以我认为的方式运作?

1 个答案:

答案 0 :(得分:3)

如果你这样做

dim mostRecentHearingDate
mostRecentHearingDate = CDate("08/05/2014")
mostRecentHearingDate = CStr(mostRecentHearingDate)

mostRecentHearingDate =“8/5/2014”而不是“08/05/2014”它掉落领先的“0”

尝试

mostRecentHearingDate = Right("0"&DatePart("m",mostRecentHearingDate),2) & "/" & Right("0"&DatePart("d",mostRecentHearingDate),2) &  "/" & DatePart("YYYY",mostRecentHearingDate)

产生

08/05/2014