首页 > 解决方案 > 如何在 VBA 中重命名 .xml 文件

问题描述

我需要在 VBA 中编辑一个 .xml 文档,并且我喜欢保留原始 xml (ppp.xml) 和单独的新编辑文件(我想调用 pppnew.xml),基本上不更新原始文件。由于我是基本用户,我从以下代码开始:

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

我试图编辑以下代码行

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

通过将其更改为

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

但它不起作用:代码更新了原始文件,我想保持未编辑。

标签: xmlvbafile-rename

解决方案


您实际上是将修改保存在 SAME 文件中

改变

Obj.Save xmlpath

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

推荐阅读