我想在VBA中引用一个Word文档中的一些CustomXML数据。我已经加载XML部分,但无法获得具体的价值。在Word中使用VBA的CustomXML提取
XML:
<?xml version="1.0"?>
<?mso-infoPathSolution solutionVersion="1.0.0.3" productVersion="14.0.0" PIVersion="1.0.0.0" href="http://portal-mysites/personal/adamh/Personal%20Documents/PropTest.xsn" name="urn:schemas-microsoft-com:office:infopath:PropTest:-myXSD-2013-07-01T14-47-53" ?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.3"?>
<my:myFields xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2013-07-01T14:47:53">
<my:tCompany>AnyCharity</my:tCompany>
<my:tCharity>true</my:tCharity>
<my:tEthernet>false</my:tEthernet>
<my:tContact>ANOther</my:tContact>
</my:myFields>
宏代码:
Sub TestPropMac()
Dim myPart As CustomXMLPart
Dim oNode As CustomXMLNode
Set myPart = GetXMLPartByRoot_Element(ActiveDocument, "myFields")
MsgBox myPart.XML
Set oNode = myPart.SelectSingleNode("myFields/tCharity")
MsgBox oNode.NodeValueEnd Sub
我使用MSGBOX确认我已经达到的数据(我没有) - 我打算使用如果声明反对另一个函数的值。
嗨!欢迎来到!你想要弄清什么具体数据?你可能想看到这个:http://stackoverflow.com/questions/11305/how-to-parse-xml-in-vba使用if语句和VBA的常规字符串匹配来解析XML之类的东西是一个很大的禁忌,尽管。 – franklin
例如,我想检查tCharity的值(当前为true) - 这将链接到InfoPath表单中的复选框,所以如果勾选框,则采取一个操作,如果不是,则采取不同的操作。 – AdamHw
我看到了......说实话,做这件事的“最好”方法可能是引用MSXML库a/k/a microsft核心xml API。它具有处理XML数据结构很有用的各种东西。但是,如果你想快速和肮脏,你只需要一个领域,那么也许调用微软的内置regexp包就足够了。你可以在这里阅读更多。 http://www.regular-expressions.info/vb.html我知道我以后会在这个方向引发你的火灾......但有时候解决这个问题是“足够好” – franklin