3

我试图在 VBA 中引用的 Word 文档中有一些 CustomXML 数据。我已经加载了 XML 部分,但无法得到具体的值。

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 来确认我已经获得了数据(我没有) - 我打算对另一个函数的值使用 If 语句。

4

1 回答 1

0

您没有显示 GetXMLPartByRoot_Element 的代码(它不在 Word 对象模型 AFAIK 中),但您需要更像这样的代码:

Sub TestPropMac()
    Const ns As String = "http://schemas.microsoft.com/office/infopath/2003/myXSD/2013-07-01T14:47:53"
    Dim myParts As CustomXMLParts
    Dim oNode As CustomXMLNode
    Dim strPrefix As String

    Set myParts = ActiveDocument.CustomXMLParts.SelectByNamespace(ns)
    MsgBox myParts(1).XML
    strPrefix = myParts(1).NamespaceManager.LookupPrefix(ns) & ":"
    Set oNode = myParts(1).SelectSingleNode(strPrefix & "myFields" & "/" & strPrefix & "tCharity")
    MsgBox oNode.Text
    Set oNode = Nothing
    Set myParts = Nothing
End Sub

strPrefix 不会是“my:”,而是 Word 生成的前缀名称,例如“ns0:”

于 2013-07-12T13:00:28.637 回答