![]() |
#1
|
|||
|
|||
![]()
I'm trying to find a way to look up values stored in a custom XML part, and as best I can figure, the method SelectByNamespace seems like the best way to make sure I have the right customXMLPart before I sort through the nodes. Does any one know a good reference on how to use this method?
The MSDN object library has the following example, but without the relevant XML script to go with it, I'm having a hard time understanding just what the ("urn:invoice:namespace") is matching to. it looks more like a namespace prefix than a URI to me but I may just be confused. HTML Code:
Dim cxp1 As CustomXMLParts Dim cxn As CustomXMLNode ' Returns all of the custom xml parts with the given namespace. Set cxp1 = ActiveDocument.CustomXMLParts.SelectByNamespace("urn:invoice:namespace") ' Get the node matching the XPath expression. Set cxn = cxp1(1).SelectSingleNode("//*[@supplierID = 1]") HTML Code:
<?xml version='1.0' ?> <data xmlns:c='comments' xmlns:a='applicant'> <a:Applicant> <a:FullName>AEP Texas Central Company</a:FullName> <a:AbbrName></a:AbbrName> <a:DateFiled></a:DateFiled> <a:Description></a:Description> </a:Applicant> </data> HTML Code:
Sub RetrieveNodeData() Dim oCustomXMLPart As CustomXMLPart Dim oNode As CustomXMLNode Dim strCustPartID As String ** Set oCustomXMLPart = ActiveDocument.CustomXMLParts.SelectByNamespace("comments") ** Set oNode = oCustomXMLPart.SelectSingleNode("/a:root/a:Applicant/a:FullName") strCustPartID = oNode.Text MsgBox (strCustPartID) End Sub |
#2
|
|||
|
|||
![]()
Maybe this will help:
PHP Code:
|
#3
|
|||
|
|||
![]()
Probably better is to use SelectbyID:
PHP Code:
|
#4
|
|||
|
|||
![]()
This may be more than you asked for, but since the VBA help for the majority of the CustomXMLPart properties and methods is a train wreck, I though I would post a working example to illustrate a few things the help fails to do:
HTML Code:
Option Explicit Sub Demo() Dim oCXParts As CustomXMLParts Dim oCXNode As CustomXMLNode Dim oCXChildNode As CustomXMLNode Dim strData As String Dim oCXNodes As CustomXMLNodes 'Demonstarates 1) The CustomXMLParts collection and adding CustomXMLParts 2) SelectByNamespace 3) SelectNodes 4) SelectSingleNode 'Provide the XML for the demonstration. AddDemoParts 'Get the collection of custom xml parts with the given namespace. (There are 2 in this demo) Set oCXParts = ActiveDocument.CustomXMLParts.SelectByNamespace("urn:invoice:namespace") 'Using the first XML part with matching namespace, get the first node matching the XPath expression (i.e., supplierID value = 1). Set oCXNode = oCXParts(1).SelectSingleNode("//*[@supplierID = 1]") 'Do something meaningful: strData = oCXNode.ParentNode.BaseName & ": " For Each oCXChildNode In oCXNode.ParentNode.ChildNodes If oCXChildNode.NodeType = msoCustomXMLNodeElement Then strData = strData & oCXChildNode.BaseName & ": " & oCXChildNode.Text & " " Else strData = strData & oCXChildNode.Text & " " End If Next oCXChildNode MsgBox Trim(strData) 'Using the second XML part with matching namespace, get the collection of all nodes matching the XPath expression (i.e., supplierID value = 1). Set oCXNodes = oCXParts(2).SelectNodes("//*[@supplierID = 1]") 'Process each node in the collection of nodes. (There are three nodes). For Each oCXNode In oCXNodes strData = oCXNode.ParentNode.BaseName & ": " For Each oCXChildNode In oCXNode.ParentNode.ChildNodes If oCXChildNode.NodeType = msoCustomXMLNodeElement Then strData = strData & oCXChildNode.BaseName & ": " & oCXChildNode.Text & " " Else strData = strData & oCXChildNode.Text & " " End If Next oCXChildNode MsgBox Trim(strData) Next oCXNode lbl_Exit: Exit Sub End Sub Sub AddDemoParts() 'Kill any existing XMLPart used previously for demo. KillPilingUpParts 'Create two different demo XML Parts with the same namespace. ActiveDocument.CustomXMLParts.Add "<?xml version='1.0' ?><Root xmlns='urn:invoice:namespace'>" _ & "<Item>Hammer<Supplier supplierID='1'><Name>Acme Tools</Name></Supplier></Item>" _ & "<Item>Cog<Supplier supplierID='2'><Name>Coswell Gogs</Name></Supplier></Item>" _ & "<Item>Sprocket<Supplier supplierID='3'><Name>Spacely Sprockets</Name></Supplier></Item>" _ & "<Item>Wrench<Supplier supplierID='1'><Name>Acme Tools</Name></Supplier></Item>" _ & "</Root>" ActiveDocument.CustomXMLParts.Add "<?xml version='1.0' ?><Root xmlns='urn:invoice:namespace'>" _ & "<Item>Toilet Paper<Supplier supplierID='1'><Name>Proctor & Gamble</Name></Supplier></Item>" _ & "<Item>Wax<Supplier supplierID='2'><Name>Johnson & Johnson</Name></Supplier></Item>" _ & "<Item>Toilet Paper<Supplier supplierID='3'><Name>Kimberly Clarke</Name></Supplier></Item>" _ & "<Item>Soap<Supplier supplierID='1'><Name>Proctor & Gamble</Name></Supplier></Item>" _ & "<Item>Shaving Cream<Supplier supplierID='1'><Name>Proctor & Gamble</Name></Supplier></Item>" _ & "</Root>" lbl_Exit: Exit Sub End Sub Sub KillPilingUpParts() Dim lngIndex As Long For lngIndex = ActiveDocument.CustomXMLParts.Count To 4 Step -1 ActiveDocument.CustomXMLParts(lngIndex).Delete Next lngIndex lbl_Exit: Exit Sub End Sub |
#5
|
|||
|
|||
![]()
Wow, thank you. I've been away from my work since Wednesday so I'm just getting back now. I think I'll be able to figure this out with what you have posted.
|
#6
|
|||
|
|||
![]()
Robster,
I've spent a little (a lot actually) or my free time this week working the demonstrations you can download here: http://gregmaxey.com/word_tip_pages/...pful_help.html I think, like you seem to do, that Microsoft's help for this topic has significant room for improvement. Maybe my efforts will save you and others some time in the future with this topic. Thanks. |
![]() |
|