首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Excel导出XML文件

从Excel导出XML文件
EN

Stack Overflow用户
提问于 2017-11-09 15:40:31
回答 1查看 1.2K关注 0票数 0

我有一个不运行的宏。

此宏用于导出.xml文件。运行此宏时,会弹出“保存as..file”。我按OK键,会出现一个错误:

运行时错误9:下标超出范围

我看到了行错误:ActiveWorkbook.XmlMaps("Root_Map").Export URL:=newFileName

谢谢你的帮助。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2017-11-09 15:50:58

超出范围错误的下标意味着变量newFileName或Root_Map没有正确定义。

定义了newFileName。测试xmlMaps。

代码语言:javascript
复制
Sub CheckXMLMaps()
Dim xmlMp As XmlMap
For Each xmlMp In ThisWorkbook.XmlMaps
    Debug.Print xmlMp.Name
Next xmlMp
End Sub

检查正在立即窗口打印的姓名。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47205912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档