我已经从工作表中的单元格加载了XML内容,在修改XML之后,我尝试将XML文件保存回工作表中的不同单元格。我在执行此操作时收到错误
Sub XMLTest01()
Dim xmlDoc As MSXML2.Dom Document
Set xmlDoc = New MSXML2.Dom Document
Dim strXML As String
Strxml = sheets("sheet2").Range("a1")
xmlDoc.LoadXML(strXML)
---- some logic ----
xmldoc.save ( sheets("sheet2").Range("a1") )
end
请告诉我如何将修改后的xml保存到单元格
答案 0 :(得分:0)
MSXML的Save方法仅用于将DOM对象保存到外部.xml文件,因此需要定义路径。考虑将其保存到临时文件,然后使用FSO将内容读取到稍后输出到单元格的字符串变量:
...
xmldoc.save "C:\Path\To\Temp.xml"
Dim objFSO As Object
Dim strXML As String
Set objFSO = CreateObject("Scripting.FileSystemObject")
strXML = objFSO.OpenTextFile("C:\Path\To\Temp.xml").ReadAll
Sheets("Sheet2").Range("A1") = strXML
或逐行显示较大的文件:
Dim strXML As String, line As String
Dim fileNo As Integer
fileNo = FreeFile
Open "C:\Path\To\Temp.xml" For Input As #fileNo
Do While Not EOF(fileNo)
Line Input #fileNo, line
strXML = strXML & line
Loop
Close #fileNo
Sheets("Sheet2").Range("A1") = strXML