[英]Exporting XML File from Excel
我有一个无法运行的宏。
此宏用于导出.xml文件。 运行此宏时,将弹出一个“另存为..文件”。 我按确定,发生错误:
“运行时错误9:下标超出范围”
我在以下行看到错误: ActiveWorkbook.XmlMaps("Root_Map").Export URL:=newFileName
感谢您的帮助。
Sub ExportXML()
'
' Export XML Macro exports the data that is in Excel to XML.
'
Const ForReading = 1
Const ForWriting = 2
Dim objFSO As Variant
Dim newFileName As Variant
Dim objFile As Variant
Dim strLine As Variant
Dim strNewContents As Variant
Set objFSO = CreateObject("Scripting.FileSystemObject")
'
newFileName = Application.GetSaveAsFilename("out.xml", "XML Files (*.xml), *.xmls")
If newFileName = False Then
Exit Sub
End If
If objFSO.FileExists(newFileName) Then
objFSO.DeleteFile (newFileName)
End If
ActiveWorkbook.XmlMaps("Root_Map").Export URL:=newFileName
Set objFile = objFSO.OpenTextFile(newFileName, ForReading)
Dim count
count = 0
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If count = 0 Then
strNewContents = strNewContents & "<?xml version=""1.0"" ?>" & vbCrLf
ElseIf count = 1 Then
strNewContents = strNewContents & "<Root xmlns=""http://tempuri.org/import.xsd"">" & vbCrLf
Else
strNewContents = strNewContents & strLine & vbCrLf
End If
count = count + 1
Loop
objFile.Close
Set objFile = objFSO.OpenTextFile(newFileName, ForWriting)
objFile.Write strNewContents
objFile.Close
End Sub
下标超出范围错误表示变量newFileName或Root_Map没有正确定义。
newFileName已定义。 测试xmlMaps。
Sub CheckXMLMaps()
Dim xmlMp As XmlMap
For Each xmlMp In ThisWorkbook.XmlMaps
Debug.Print xmlMp.Name
Next xmlMp
End Sub
检查在立即窗口中打印的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.