speakers_86
Registered User.
- Local time
- Yesterday, 19:51
- Joined
- May 17, 2007
- Messages
- 1,919
Heres my code
The error at line:
Is Err 91, With block or object variable not set. That tells me that node is node is not getting set correctly by the XPath. Any ideas?
Code:
Sub test()
Dim objdom As DOMDocument
Dim objRootElem As IXMLDOMElement
Set objdom = New DOMDocument
' Creates root element
Set objRootElem = objdom.CreateElement("Books")
objdom.appendChild objRootElem
Dim node As IXMLDOMElement
Debug.Print "a"
Set node = CreateNode("Books", "aNewNode", objdom)
Debug.Print "b"
Set node = CreateNode("//Books/aNewNode/", "Test", objdom)
Debug.Print "c"
CreateAttrib node, "StarWars", "ANewHope", objdom
Dim test As IXMLDOMElement
Set test = objdom.selectSingleNode("//aNewNode[@StarWars='ANewHope']")
CreateAttrib test, "ANewAttribute", "SomeValue", objdom 'breaks it
Debug.Print "d"
CreateAttrib node, "id", "GreatMovies", objdom
Debug.Print "e"
'CreateNode "GreatMovies", "ChildTest", objdom, True
' Saves XML data to disk.
objdom.Save ("C:\Users\Kanyer\Desktop\andrew.xml")
Set objdom = Nothing
Set objRootElem = Nothing
End Sub
Code:
Public Sub CreateAttrib(node As IXMLDOMElement, _
strAttrib As String, _
strValue As String, _
objdom As DOMDocument)
'Create an attribute in the specified dom and node
'Parameters
' strNode enter the name of the node to modify
' strAttrib enter the name of your attribute
' strValue enter the value the attribute will assume
' objdom reference to the document
'example <strNode strAttrib=strValue/>
'Adapted from http://www.freevbcode.com/ShowCode.asp?ID=1919
'by speakers_86
Dim attrib As IXMLDOMAttribute
Set attrib = objdom.createAttribute(strAttrib)
attrib.nodeValue = strValue
node.setAttributeNode attrib 'This line is highlighted
'Set node = Nothing
Set attrib = Nothing
End Sub
Code:
Public Function CreateNode(strParent As String, _
strNode As String, _
objdom As DOMDocument, _
Optional ByID As Boolean) As IXMLDOMElement
'Add a node to the specified dom
'Parameters
' strParent node will be created as a child of this node
' strNode basename of your node; ex. <strNode\>
' objdom reference to the document
'example <strParent><strNode></strParent>
'Adapted from http://www.freevbcode.com/ShowCode.asp?ID=1919
'by speakers_86
Dim node As IXMLDOMNode
Dim parent As IXMLDOMElement
Dim nodeList As IXMLDOMNodeList
If ByID = True Then
Set nodeList = objdom.selectNodes(strParent)
Set parent = nodeList(1)
Else
Set parent = objdom.selectSingleNode(strParent)
End If
Set node = objdom.CreateElement(strNode)
Set CreateNode = parent.appendChild(node)
Set parent = Nothing
Set node = Nothing
On Error Resume Next
Set nodeList = Nothing
End Function
The error at line:
Code:
node.setAttributeNode attrib
Last edited: