使用 Excel VBA 创建 XML 文件

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

Using Excel VBA to create an XML file

xmlexcelvba

提问by user2151763

I'm trying to create an XML file from VBA found in Excel, I'm a little hazy on the details though, I've never touched XML or VBA before in my life. I need to write out an XML file with in the following format:

我正在尝试从 Excel 中的 VBA 创建一个 XML 文件,虽然我对细节有点模糊,但我以前从未接触过 XML 或 VBA。我需要用以下格式写出一个 XML 文件:

<TextField0 xfdf:original="Brand Name">HEADING1</TextField0>

There will be a bunch of lines like this such as TextField1, which is "Product" and so on. The code I have thus far revelent to this section is this:

会有一堆这样的行,比如TextField1,它是“产品”等等。到目前为止,我对本节感兴趣的代码是:

   'create Heading element
   Set objXMLelement = objDom.createElement("TextField0")
   objXMLRootelement.appendChild objXMLelement

   'create Attribute to the Heading Element and set value
   Set objXMLattr = objDom.createAttribute("xfdf:original")
   objXMLattr.NodeValue = "Brand Name"
   objXMLelement.setAttributeNode objXMLattr

That creates this output:

这将创建此输出:

<TextField0 xfdf:original="Brand Name"/>

This is of coursem missing HEADING1 and I can't for the life of me figure out how to put that bit in there. I can't seem to append anything to an attribute. Any help will be gratefully received.

这当然是缺少 HEADING1 并且我一生都无法弄清楚如何将其放入其中。我似乎无法向属性附加任何内容。任何帮助将不胜感激。

回答by Joe

HEADING1 is the text that belongs to the TextField0element, not the attribute.

HEADING1 是属于TextField0元素的文本,而不是属性。

So you can set it using the objXmlElement.textproperty.

所以你可以使用objXmlElement.text属性来设置它。

In response to comment:

回应评论:

I'm struggling to find documentation for this stuff

我正在努力寻找这些东西的文档

This MSDN pageis a good start.

这个 MSDN 页面是一个好的开始。