View Single Post
 
Old 06-13-2017, 06:47 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 am far from an expert in this arena and agree with your "bit of a black box," but this aspect isn't as mystical as it may seem.

It isn't that Words CustomXMLParts insists on creating prefixes, it is if your XML uses a names space then to access the nodes you must reference the namespace prefix. If you don't name it something then Word automatically uses the default namespace prefix ns0.

Run this little demo and you can see for yourself:

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 with an unprefixed namespace.
  strXML = "<Root xmlns='SomeNameSpace'><NodeA></NodeA><NodeB></NodeB></Root>"
  Set oXMLPart = ActiveDocument.CustomXMLParts.Add(strXML)
  'Then you must use the default namespace prefix.
  oXMLPart.SelectSingleNode("/*/ns0:NodeA[1]").Text = "Some Value"
  MsgBox oXMLPart.SelectSingleNode("/*/ns0:NodeA[1]").Text
  On Error GoTo Err_Handler:
    oXMLPart.SelectSingleNode("/*/NodeB[1]").Text = "Some Value"
    MsgBox oXMLPart.SelectSingleNode("/*/NodeA[B]").Text
lbl_ReEntry:
  Stop
  'Create a simple XMLPart without a namespace:
  strXML = "<Root><NodeA></NodeA><NodeB></NodeB></Root>"
  Set oXMLPart = ActiveDocument.CustomXMLParts.Add(strXML)
  oXMLPart.SelectSingleNode("/*/NodeA[1]").Text = "See, no namespace prefix required"
  MsgBox oXMLPart.SelectSingleNode("/*/NodeA[1]").Text
lbl_Exit:
  Exit Sub
Err_Handler:
  MsgBox Err.Number & " " & Err.Description
  Resume lbl_ReEntry
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote