PDA

View Full Version : Solved: Reading CustomXML



Marcster
06-13-2011, 10:19 AM
Hi,

Does anyone know how to read CustomXML?.

I can set CustomXML with:


Sub AddPartToCollection()
Dim myPart As CustomXMLPart
Set myPart = ActiveDocument.CustomXMLParts.Add("<author>Marcster</author>")
End Sub

But how to read it?.

There is CustomXMLParts.Item Property which I can use if I know the Item number. How do I refer to it by name?.

If I run the above sub, how can I read it again by name?, like 'author'.

Thanks,

gmaxey
06-14-2011, 05:14 AM
See if this helps:

Option Explicit
Sub AddPartToCollection()
Dim myPart As CustomXMLPart
Set myPart = ActiveDocument.CustomXMLParts.Add("<author>Marcster</author>") 'simple part with root element only
Set myPart = ActiveDocument.CustomXMLParts.Add("<authors><author>Marcster</author><author>Jones</author></authors>") 'simple part with root element and nodes
Set myPart = ActiveDocument.CustomXMLParts.Add("<?xml version='1.0' encoding='utf-8'?><myinfo xmlns='http://gremaxey.mvps.org/CustomXML'>" _
& "<email>Click to enter e-mail</email><motto>Add your favorite motto</motto></myinfo>") 'simple part with namespace, root element and nodes
Set myPart = ActiveDocument.CustomXMLParts.Add("<cars>Mustang</cars>") 'simple part with root element only
ActiveDocument.Variables("myPartID").Value = myPart.ID
End Sub
Sub ReadXMLPart()
Dim myPart As CustomXMLPart
Dim oNode As CustomXMLNode
Set myPart = GetXMLPartByRoot_Element(ActiveDocument, "author")
'Part has only root element
MsgBox myPart.DocumentElement.Text
Set myPart = GetXMLPartByRoot_Element(ActiveDocument, "authors")
'Read node 1
Set oNode = myPart.SelectSingleNode("/authors/author[1]")
MsgBox oNode.Text
'Read node 2
Set oNode = myPart.SelectSingleNode("/authors/author[2]")
MsgBox oNode.Text
Set myPart = GetXMLPartBySchema(ActiveDocument, "http://gremaxey.mvps.org/CustomXML")
'Read node 1
Set oNode = myPart.SelectSingleNode("ns0:myinfo/ns0:email")
MsgBox oNode.Text
Set myPart = ActiveDocument.CustomXMLParts.SelectByID(ActiveDocument.Variables("myPartID").Value)
MsgBox myPart.DocumentElement.Text
End Sub
Public Function GetXMLPartByRoot_Element(docContainer As Document, pRootElement As String) As CustomXMLPart
Dim oCustXMLPart As CustomXMLPart
For Each oCustXMLPart In docContainer.CustomXMLParts
If oCustXMLPart.DocumentElement Is Nothing Then
oCustXMLPart.Delete
ElseIf pRootElement = oCustXMLPart.SelectSingleNode("/*").BaseName Then
Set GetXMLPartByRoot_Element = oCustXMLPart
Exit Function
End If
Next
End Function
Public Function GetXMLPartBySchema(docContainer As Document, pSchemaURI As String) As CustomXMLPart
Dim oCustXMLPart As CustomXMLPart
For Each oCustXMLPart In docContainer.CustomXMLParts
If oCustXMLPart.NamespaceURI = pSchemaURI Then
Set GetXMLPartBySchema = oCustXMLPart
Exit Function
End If
Next
End Function
Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim i As Long
For i = ActiveDocument.CustomXMLParts.Count To 4 Step -1
ActiveDocument.CustomXMLParts(i).Delete
Next i
End Sub

Marcster
06-14-2011, 09:55 AM
Thanks Greg. Helped with the whole CustomXML thing too :-).