I'm currently having an issue trying to delete xml nodes from a file in VBA. I've been using the object browser to find functions to use for XML manipulation, and everything except deleting nodes has worked so far. For deleting I'm trying to use the method described here:
https://learn.microsoft.com/en-us/office/vba/api/office.customxmlnode.delete
Simplified Sample code:
Set xmlfile = CreateObject("MSXML2.DOMDocument.6.0")
xmlfile.async = False
If Not xmlfile.Load(*xml filename here*) Then
MsgBox ("xml load error")
End
End If
Set PVar = xmlfile.SelectSingleNode(*Node address*)
PVar.Delete
The last line will throw up a run time error 438, "Object doesn't support this property or method"
One issue I have considered may be causing my problem is according to this [https://learn.microsoft.com/en-us/office/vba/api/office.customxmlnode.selectsinglenode] Set PVar = .... should return a CustomXMLNode object but instead when I run
Debug.Print TypeName(PVar)
It returns IXMLDOMElement. If I dimension PVar as a CustomXMLNode ahead of time I get a type mismatch error when the program tries to execute the Set PVar = .... line.
Any ideas on how to resolve this? I like the idea of the Delete function I've linked as it seems like a very clean solution for what I need to do, but I don't understand why it isn't working as applied.
XML nodes do not have a Delete
method - the typical way to remove a node is to remove it from its parent node using RemoveChild
.
So:
PVar.ParentNode.RemoveChild PVar
should work.