使用 VBA 的 XMLImport 和 ImportMap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14691962/
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:27:38  来源:igfitidea点击:

XMLImport and ImportMap Using VBA

xmlexcelexcel-vbaxsdxmlmappervba

提问by aakbari55

Here is my issue..

这是我的问题..

I currently have a code built so I can import my XML data via the URL. The problem is when I use this code it drops off any columns that have no data. I need those columns to be included because I have pre-set formulas that populate other sheets with data. I figured I would need an XML mapping in order to do so. I then created a code so the XML Mapping is brought into the file via the XSD URL. After walking through the VBA step by step, I noticed the correct XSD mapping is brought into the file via the code below:

我目前已经构建了一个代码,因此我可以通过 URL 导入我的 XML 数据。问题是当我使用此代码时,它会删除任何没有数据的列。我需要包含这些列,因为我有使用数据填充其他工作表的预设公式。我想我需要一个 XML 映射才能这样做。然后我创建了一个代码,以便通过 XSD URL 将 XML 映射引入到文件中。在一步一步地完成 VBA 之后,我注意到正确的 XSD 映射通过以下代码被引入到文件中:

Dim strRestXSD As String 'References URL to retrieve XSD
Dim oMap As xmlmap
Set oMap = ActiveWorkbook.XmlMaps.Add(strRestXSD)

The problem is within the code below. I use the code below to then extract the XML, using the string attached to the XML URL. For whatever reason, the ImportMap is ignored when wanting to using the XMLMap identified above. The code below actually creates a new map and ignores the map which is created using the code above.

问题出在下面的代码中。我使用下面的代码提取 XML,使用附加到 XML URL 的字符串。无论出于何种原因,当想要使用上面标识的 XMLMap 时,都会忽略 ImportMap。下面的代码实际上创建了一个新地图并忽略了使用上面的代码创建的地图。

Dim strRestURL As String XML data

ActiveWorkbook.XmlImport URL:=strRestURL, ImportMap:=oMap, Overwrite:=False,
Destination:=Range("A" & lngRow)

Is anyone aware of a way I can import my XML data without losing the empty columns? Or are you aware of a way I can map my XML map to the XML Import?

有没有人知道我可以在不丢失空列的情况下导入我的 XML 数据的方法?或者你知道我可以将我的 XML 映射映射到 XML 导入的方法吗?

Thanks in advance for your help!

在此先感谢您的帮助!

Ali Akbari

阿里·阿克巴里

回答by Scott

Setting Destination:=Nothing should get the data to import into the existing map. Also, you may want to set Overwrite:=True; otherwise, the data in the existing mapped table will be appended to.

Setting Destination:=Nothing 应该将数据导入到现有地图中。此外,您可能需要设置 Overwrite:=True; 否则,将附加现有映射表中的数据。