vba 将 XML 文件作为 XML 表而不是只读工作簿打开的 EXCEL 宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14257552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:08:19  来源:igfitidea点击:

EXCEL Macro to open XML files as a XML table rather than a read only workbook

excelexcel-vbavba

提问by Anand

I'm using the following code to open multiple xml files, however they are opening as a read only workbook, however I require it to open as an XML table, any suggestions?

我正在使用以下代码打开多个 xml 文件,但是它们是作为只读工作簿打开的,但是我需要它作为 XML 表打开,有什么建议吗?

Code:

代码:

Sub AllFolderFiles()
    Dim wb As Workbook
    Dim TheFile As String
    Dim MyPath As String
    MyPath = "C:\Documents and Settings\"
    ChDir MyPath
    TheFile = Dir("*.xml")
    Do While TheFile <> ""
        'Call Logs 'This calls for Macro2 to run
        Set wb = Workbooks.Open(MyPath & "\" & TheFile)
        MsgBox wb.FullName
        'wb.Close
        TheFile = Dir
    Loop
End Sub

采纳答案by Nick Perkins

You need to use Workbooks.OpenXMLinstead

你需要Workbooks.OpenXML改用

Set wb = Workbooks.OpenXML(Filename:=MyPath & "\" & TheFile, LoadOption:=xlXmlLoadImportToList)

Set wb = Workbooks.OpenXML(Filename:=MyPath & "\" & TheFile, LoadOption:=xlXmlLoadImportToList)

I'm not exactly which LoadOption you want to use, but you can choose from:

我不完全是您要使用的 LoadOption,但您可以选择:

  • xlXmlLoadImportToListAutomatically creates an XML List and imports data into the list.
  • xlXmlLoadMapXmlLoads the XML file into the XML Source task pane.
  • xlXmlLoadOpenXmlOpen XML files in the same way that Excel 2002 opens XML files (for backwards compatibility only).
  • xlXmlLoadPromptUserPrompts the user and lets them choose the Import method.
  • xlXmlLoadImportToList自动创建一个 XML 列表并将数据导入到列表中。
  • xlXmlLoadMapXml将 XML 文件加载到 XML 源任务窗格中。
  • xlXmlLoadOpenXml以与 Excel 2002 打开 XML 文件相同的方式打开 XML 文件(仅用于向后兼容)。
  • xlXmlLoadPromptUser提示用户并让他们选择导入方法。