我有一个不运行的宏。
此宏用于导出.xml文件。运行此宏时,会弹出“保存as..file”。我按OK键,会出现一个错误:
运行时错误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发布于 2017-11-09 15:50:58
超出范围错误的下标意味着变量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检查正在立即窗口打印的姓名。
https://stackoverflow.com/questions/47205912
复制相似问题