3

I have some CustomXML data in a Word document that I'm trying to reference in VBA. I have got as far as loading the XML part, but can't get the specific value out.

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>

Macro code:

 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

I'm using MsgBox to confirm I've reached the data (which I haven't) - I intend to use an If statement against the value for another function.

Fiona - myaccessible.website
  • 14,481
  • 16
  • 82
  • 117
AdamHw
  • 31
  • 1
  • 2
  • Hi! welcome to se! what specific data are you trying to get out? You may want to see this: http://stackoverflow.com/questions/11305/how-to-parse-xml-in-vba Using if statements and VBA's regular string matching for parsing things like XML is a big no-no though. – franklin Jul 04 '13 at 14:36
  • For example, I want to check the value of tCharity (which is currently true) - this would be linked to a checkbox in an InfoPath form, so if the box is ticked, take one action, if it's not, take a different one. – AdamHw Jul 04 '13 at 15:18
  • I see...honestly the "best" way to do this is probably to reference the MSXML library a/k/a the microsft core xml API. It has all kinds of stuff that is useful in dealing with XML data structures. BUT...if you want quick and dirty and you need just the one field then maybe a call to microsoft's built in regexp package is enough. You can read more here. http://www.regular-expressions.info/vb.html I know I'm going to catch fire later for guiding you in this direction...but sometimes solving the problem is "good enough" – franklin Jul 04 '13 at 15:46

1 Answers1

0

You don't show the code for GetXMLPartByRoot_Element (it's not in the Word object model AFAIK), but you need something more like this:

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 will not be "my:" but will be a Word-generated prefix name such as "ns0:"

  • Hey, I am facing a similar situation but I am using C# and in C# you can not use the CustomXMLParts(myParts) as a method. There is no .item method either. Any help regarding that? – Zunair Zubair May 27 '14 at 05:28
  • 1
    Depends on the version of C# and the Framework, I think , but you probably need to get a reference to the .doc, then use .CustomXMLParts[index] to get an individual part. –  May 27 '14 at 15:55