Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-21-2012, 11:52 AM
RobsterCraw RobsterCraw is offline CustomXMLParts.SelectByNamespace Windows 7 32bit CustomXMLParts.SelectByNamespace Office 2010 32bit
Novice
CustomXMLParts.SelectByNamespace
 
Join Date: Nov 2012
Posts: 11
RobsterCraw is on a distinguished road
Default CustomXMLParts.SelectByNamespace

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]")
My .xml looks like this:
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>
And my Function looks mike this:
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
I get a runtime error '13': Type Mismatch on the line with the **
Reply With Quote
  #2  
Old 11-21-2012, 01:44 PM
gmaxey gmaxey is offline CustomXMLParts.SelectByNamespace Windows 7 32bit CustomXMLParts.SelectByNamespace Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,439
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Maybe this will help:

PHP Code:
Sub DemoXYZ()
Dim oDoc As Word.Document
Dim oCustXMLPart As Office.CustomXMLPart
Dim oNode As CustomXMLNode
Dim lngIndex As Long
  Set oDoc = ActiveDocument
  'Kill any existing XMLPart used previously for mapping.
  On Error Resume Next
  oDoc.CustomXMLParts.SelectByNamespace("http://nsDemo").Item(1).Delete
  On Error GoTo 0
  'Create a basic XML Part with namespace.
  Set oCustXMLPart = ActiveDocument.CustomXMLParts.Add("<?xml version='1.0' encoding='utf-8'?><Root_Node xmlns='http://nsDemo'></Root_Node>")
  For lngIndex = 1 To 3
    oCustXMLPart.AddNode oCustXMLPart.SelectSingleNode("/ns0:Root_Node"), "Node_" & lngIndex
    Set oNode = oCustXMLPart.SelectSingleNode("/ns0:Root_Node[1]/Node_" & lngIndex & "[1]")
    oNode.Text = "Demo text " & lngIndex
  Next lngIndex
  'This is what you've got:
  Debug.Print oCustXMLPart.XML
  Set oCustXMLPart = Nothing
  'Set part by namespace
  Set oCustXMLPart = oDoc.CustomXMLParts.SelectByNamespace("http://nsDemo").Item(1)
  For Each oNode In oCustXMLPart.DocumentElement.ChildNodes
    Debug.Print "My text is: " & oNode.Text & ". My xPath is: " & oNode.XPath
  Next oNode
  'Or
  Set oNode = oCustXMLPart.SelectSingleNode("/ns0:Root_Node/Node_1[1]")
  MsgBox oNode.Text
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 11-21-2012, 02:52 PM
gmaxey gmaxey is offline CustomXMLParts.SelectByNamespace Windows 7 32bit CustomXMLParts.SelectByNamespace Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,439
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Probably better is to use SelectbyID:

PHP Code:
Sub DemoXX()
Dim oDoc As Word.Document
Dim oCustXMLPart As Office.CustomXMLPart
  Set oDoc = ActiveDocument
  'Kill any existing XMLPart used previously for demo.
  On Error Resume Next
    oDoc.CustomXMLParts.SelectByID(ActiveDocument.Variables("Part_ID")).Delete
  On Error GoTo 0
  'Create a basic XML Part with namespace.
  Set oCustXMLPart = ActiveDocument.CustomXMLParts.Add("<?xml version='1.0' ?><data xmlns='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>")
  'Store the ID value
  ActiveDocument.Variables("Part_ID").Value = oCustXMLPart.ID
  Set oCustXMLPart = Nothing
  RetrieveNodeData
End Sub
Sub RetrieveNodeData()
Dim oCustomXMLPart As CustomXMLPart
Dim oNode As CustomXMLNode
  Set oCustomXMLPart = ActiveDocument.CustomXMLParts.SelectByID(ActiveDocument.Variables("Part_ID"))
  'Or
  'Set oCustomXMLPart = ActiveDocument.CustomXMLParts.SelectByNamespace("comments").Item(1)
  Set oNode = oCustomXMLPart.SelectSingleNode("ns0:data[1]/ns1:Applicant[1]/ns1:FullName[1]")
  MsgBox oNode.Text
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #4  
Old 11-22-2012, 12:22 PM
gmaxey gmaxey is offline CustomXMLParts.SelectByNamespace Windows 7 32bit CustomXMLParts.SelectByNamespace Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,439
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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 &amp; Gamble</Name></Supplier></Item>" _
                                  & "<Item>Wax<Supplier supplierID='2'><Name>Johnson &amp; Johnson</Name></Supplier></Item>" _
                                  & "<Item>Toilet Paper<Supplier supplierID='3'><Name>Kimberly Clarke</Name></Supplier></Item>" _
                                  & "<Item>Soap<Supplier supplierID='1'><Name>Proctor &amp; Gamble</Name></Supplier></Item>" _
                                  & "<Item>Shaving Cream<Supplier supplierID='1'><Name>Proctor &amp; 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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 11-25-2012, 05:07 PM
RobsterCraw RobsterCraw is offline CustomXMLParts.SelectByNamespace Windows 7 32bit CustomXMLParts.SelectByNamespace Office 2010 32bit
Novice
CustomXMLParts.SelectByNamespace
 
Join Date: Nov 2012
Posts: 11
RobsterCraw is on a distinguished road
Default Thanks Shipmate!

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.
Reply With Quote
  #6  
Old 12-01-2012, 08:08 PM
gmaxey gmaxey is offline CustomXMLParts.SelectByNamespace Windows 7 32bit CustomXMLParts.SelectByNamespace Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,439
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



Other Forums: Access Forums

All times are GMT -7. The time now is 05:05 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft