View Single Post
 
Old 06-15-2017, 04:45 PM
gmaxey gmaxey is offline Windows 7 32bit Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

SC,

I tinkered a bit and thought I would share the findings:

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim strXML As String
Dim oXMLPart As CustomXMLPart
Dim lngIndex As Long
  For lngIndex = ActiveDocument.CustomXMLParts.Count To 4 Step -1
    ActiveDocument.CustomXMLParts(lngIndex).Delete
  Next
  'Create a simple XMLPart without a namespace.
  strXML = "<Root><NodeA>No namespace prefix required in XPath</NodeA></Root>"
  Set oXMLPart = ActiveDocument.CustomXMLParts.Add(strXML)
   MsgBox oXMLPart.SelectSingleNode("/*/NodeA[1]").Text
  'Create a simple XMLPart with an unprefixed default namespace.
  strXML = "<Root xmlns='SomeNameSpace'><NodeA>Default prefix ns0 required in xPath</NodeA></Root>"
  Set oXMLPart = ActiveDocument.CustomXMLParts.Add(strXML)
  'Then you must use the default namespace prefix.
  MsgBox oXMLPart.SelectSingleNode("/*/ns0:NodeA[1]").Text
  'Create a simple XMLPart with a prefixed namespace:
  strXML = "<Root xmlns:a='SomeNameSpace'><a:NodeA>You can use the default prefix ns0</a:NodeA><a:NodeB></a:NodeB></Root>"
  Set oXMLPart = ActiveDocument.CustomXMLParts.Add(strXML)
  MsgBox oXMLPart.SelectSingleNode("/*/ns0:NodeA[1]").Text
  On Error Resume Next
  MsgBox oXMLPart.SelectSingleNode("/*/a:NodeC[1]").Text
  If Err.Number <> 0 Then
    oXMLPart.NamespaceManager.AddNamespace "a", "SomeNameSpace"
    oXMLPart.SelectSingleNode("/*/a:NodeA[1]").Text = "Before using your custom namespace prefixe you must add it to the XMLPart with the NamespaceManager"
     MsgBox oXMLPart.SelectSingleNode("/*/a:NodeA[1]").Text
  End If
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote