Search code examples
xmlvbafile-rename

How to rename an .xml file in VBA


I need to edit an .xml document in VBA, and I like to keep the original xml (ppp.xml) and separately the new edited file (which I want to call pppnew.xml), basically without updating the original file. Since I am a basic user, I started with the following piece of code:

Sub editxml()

Dim Obj As MSXML2.DOMDocument
Dim xmlpath As String

Set Obj = New DOMDocument
Obj.async = False
Obj.validateOnParse = False

xmlpath = "C:\Users\xxx\Desktop\ppp.xml"
Obj.SetProperty "SelectionNamespaces", "xmlns:ns0='http://update.DocumentTypes.Schema.ppp.Xml'"

If Obj.Load(xmlpath) = True Then
    MsgBox "File XML uploaded"
Else
    MsgBox "File XML not uploaded"
    Exit Sub
End If
        
        'My code follows here
    
Obj.Save xmlpath
End sub

I've tried to edit the following line of the code

SelectionNamespaces", "xmlns:ns0='http://update.DocumentTypes.Schema.ppp.Xml'

By changing it into

SelectionNamespaces", "xmlns:ns0='http://pppnew.Xml'

But it doesn't work: the code updates the original file, which I want to keep unedited.


Solution

  • You are in fact saving your modifications on the SAME file

    Change

    Obj.Save xmlpath
    

    To

    new_xmlpath = "C:\Users\xxx\Desktop\new.xml"
    Obj.Save new_xmlpath