1

I'm learning VBScript and I am trying to modify values on a xml file using vbscript but I always get an error with the variable "colNode":

Microsoft VBScript Runtime Error: This object does not support this property or method: 'colNode.Text

Here is my XML file:

<?xml version="1.0" encoding="utf-8"?>
<Configuration>
    <Text Name="Text1">Lorem ipsum dolor sit amet, consectetur adipiscing elit. </Text>
    <Text Name="Text2">Aliquam mattis quam lorem, ut sollicitudin dolor dignissim sed. </Text>
    <Text Name="Text3">Fusce cursus tellus eu consectetur rutrum.</Text>
</Configuration>

And here is my VBScript code:

    Set xmlDoc = _
    CreateObject("Microsoft.XMLDOM")

    xmlDoc.Async = "False"
    xmlDoc.Load(".\message.xml")

    
    Set colNode=xmlDoc.selectNodes _
  ("//Configuration/Text[@Name='Text2']")

    
    colNode.Text = "It's a test!"  
    xmlDoc.Save "C:\Scripts\Audits.xml"
3
  • 2
    Try Set colNode=xmlDoc.SelectSingleNode("//Configuration/Text[@Name='Text2']") Commented Apr 30, 2021 at 9:33
  • Try it and get the following error message: Required object: 'colNode Commented Apr 30, 2021 at 9:38
  • Same error message Required object: 'colNode(...)' Commented Apr 30, 2021 at 9:39

1 Answer 1

2

The SelectNodes method returns a collection of nodes (in this case a collection of just one) and if you want to use that, you need to iterate through that collection in order to change the text in every node in the collection:

Set colNode=xmlDoc.SelectNodes("//Configuration/Text[@Name='Text2']")
For Each node In colNode
    node.Text = "It's a test!"
Next

In your example however, you are seeking to change the text in only one specific node, so for that you ca use the SelectSingleNode method:

Set colNode=xmlDoc.SelectSingleNode("//Configuration/Text[@Name='Text2']")
colNode.Text = "It's a test!"
Sign up to request clarification or add additional context in comments.

4 Comments

I got the following error message: Required object: 'colNode'
@Noemie That's because your XPath query selects nothing, so colNode will not be set properly. (In other words, the sample XML from your question is different from your real XML.)
I try with "SelectSingleNode" (uppercase S) and it works
@Noemie If I had to guess, your real XML is in a namespace (xmlns="...") and you edited that out because you thought it was not important.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.