ONode is set to Nothing, but why and how to fix it?
I am trying to develop a token (in this case a piece of code that runs inside a larger VBScript) that returns information from XML supplied by third party software to a word plugin using bookmarks to provide parameters for the tokens.
So here's what's going on,
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
Everything works the way I want, as long as
Set oNode = XmlDoc.selectSingleNode("/Record/CelloXml/Integration/Case/Hearing/Setting[HearingDate/text()='" & mostRecentHearingDate & "']/CourtroomMinutes/Comment")
I needed dateList
to store dates (or literary dates) because I assumed I was getting a bad sort if I tried to sort dates as a string rather than an actual date, so I converted the text from node to date (or date) and added it todateList
When I finished all calculations, I needed a string to run in my XPath, if I hard copy the date (as string {08/05/2014}) into the XPath query, it works, but when I convert mostRecentHearingDate
to string with CStr
then oNode
set to Nothing
node exists and contains data
So,
- Why is this happening?
- How do I get it to function the way it seems to me?
source to share
If you do
dim mostRecentHearingDate
mostRecentHearingDate = CDate("08/05/2014")
mostRecentHearingDate = CStr(mostRecentHearingDate)
mostRecentHearingDate = "8/5/2014" and not "08/05/2014" it drops the leading "0"
try it
mostRecentHearingDate = Right("0"&DatePart("m",mostRecentHearingDate),2) & "/" & Right("0"&DatePart("d",mostRecentHearingDate),2) & "/" & DatePart("YYYY",mostRecentHearingDate)
This gives
08/05/2014
source to share