Class XMLMapping (Word VBA)
The class XMLMapping represents the XML mapping on a ContentControl object between custom XML and a content control. An XML mapping is a link between the text in a content control and an XML element in the custom XML data store for this document. To use a XMLMapping class variable it first needs to be instantiated, for example
Dim xml as XMLMapping
Set xml = ActiveDocument.Range.ContentControls(1).XMLMapping
CustomXMLNode
Returns a CustomXMLNode object that represents the custom XML node in the data store to which the content control in the document maps.
Dim objCC As ContentControl
Dim objPart As CustomXMLPart
Dim objNode As CustomXMLNode
Dim objMap As XMLMapping
Set objCC = ActiveDocument.ContentControls.Add(wdContentControlText)
Set objPart = ActiveDocument.CustomXMLParts.Add("" & _
" " & _
" ")
Set objMap = objCC.XMLMapping
objMap.SetMapping "/books/book/author", , objPart
Set objNode = objMap.CustomXMLNode
objNode.Text = "Matt Hink"
objCC.Range.Text = objNode.Text
CustomXMLPart
Returns a CustomXMLPart object that represents the custom XML part to which the content control in the document maps.
Dim objCC As ContentControl
Dim objPart As CustomXMLPart
Dim objNode As CustomXMLNode
Set objCC = ActiveDocument.ContentControls(1)
Set objPart = objCC.XMLMapping.CustomXMLPart
Set objNode = objPart.SelectSingleNode("/books/book/title")
objNode.Text = "Mystery of the Empty Chair"
Delete
Deletes the XML mapping from the parent content control.
This operation removes the XML mapping. Both the XML data and the content control remain in the document.
Dim objCC As ContentControl
For Each objCC In ActiveDocument.ContentControls
If objCC.XMLMapping.IsMapped Then
objCC.XMLMapping.Delete
End If
Next
IsMapped
Returns a Boolean that represents whether the content control in the document is mapped to an XML node in the document's XML data store.
Dim objCC As ContentControl
For Each objCC In ActiveDocument.ContentControls
If objCC.XMLMapping.IsMapped Then
objCC.XMLMapping.Delete
End If
Next
PrefixMappings
Returns a String that represents the prefix mappings used to evaluate the XPath for the current XML mapping.
To set mapping for a content control, use the SetMapping method or the SetMappingByNode method.
Dim strPrefixMappings As String
strPrefixMappings = ActiveDocument.Range.ContentControls(1).XMLMapping.PrefixMappings
SetMapping
Allows creating or changing the XML mapping on a content control. Returns True if Microsoft Word maps the content control to a custom XML node in the document's custom XML data store.
If the XML mapping already exists, Word replaces the existing XML mapping and the contents of the new mapped XML node replaces the text of the content control. If the specified XPath does not evaluate to an XML node in the specified custom XML part or parts, you can still specify the mapping, and one will be created. This mapping automatically links when the specified XPath would evaluate to an XML node in the specified custom XML parts. See also the SetMappingByNode method.
SetMapping (XPath, PrefixMapping, Source)
Dim objRange As Range
Dim objCustomPart As CustomXMLPart
Dim objCustomControl As ContentControl
Set objCustomPart = ActiveDocument.CustomXMLParts.Add
objCustomPart.LoadXML ("Matt Hink " & _
"Migration Paths of the Red Breasted Robin " & _
"non-fiction 29.95 " & _
"2/1/2007 You see them in " & _
"the spring outside your windows. You hear their lovely " & _
"songs wafting in the warm spring air. Now follow the path " & _
"of the red breasted robin as it migrates to warmer climes " & _
"in the fall, and then back to your back yard in the spring." & _
" ")
ActiveDocument.Range.InsertParagraphBefore
Set objRange = ActiveDocument.Paragraphs(1).Range
Set objCustomControl = ActiveDocument.ContentControls _
.Add(wdContentControlText, objRange)
objCustomControl.XMLMapping.SetMapping _
"/books/book/title", , objCustomPart
objRange.InsertParagraphAfter
Set objRange = ActiveDocument.Paragraphs(2).Range
Set objCustomControl = ActiveDocument.ContentControls _
.Add(wdContentControlText, objRange)
objCustomControl.XMLMapping.SetMapping _
"/books/book/abstract", , objCustomPart
Arguments
The following argument is required
XPath (String) - Specifies an XPath string that represents the XML node to which to map the content control. An invalid XPath string causes a run-time error.
Optional arguments
The following arguments are optional
PrefixMapping (String) - Specifies the prefix mappings to use when querying the expression provided in the XPath parameter. If omitted, Word uses the set of prefix mappings for the specified custom XML part in the current document.
Source (Office.CustomXMLPart) - Specifies the desired custom XML data to which to map the content control. If this parameter is omitted, the XPath is evaluated against all custom XML in the current document, and the mapping is established with the first CustomXMLPart in which the XPath resolves to an XML node.
SetMappingByNode
Allows creating or changing the XML data mapping on a content control. Returns True if Microsoft Word maps the content control to a custom XML node in the document's custom XML data store.
If the XML mapping already exists, then Word replaces the existing XML mapping, and the contents of the new mapped XML node replaces the text of the content control. See also the SetMapping method.
SetMappingByNode (Node)
Node: Specifies the XML node to which to map the current content control.
Dim objcc As ContentControl
Dim objNode As CustomXMLNode
Dim objMap As XMLMapping
Dim booMap As Boolean
ActiveDocument.BuiltInDocumentProperties("Author").Value = "David Jaffe"
Set objcc = ActiveDocument.ContentControls.Add _
(wdContentControlDate, ActiveDocument.Paragraphs(1).Range)
Set objNode = ActiveDocument.CustomXMLParts.SelectByNamespace _
("https://schemas.openxmlformats.org/package/2006/metadata/core-properties") _
(1).DocumentElement.ChildNodes(1)
Set objMap = objcc.XMLMapping
booMap = objMap.SetMappingByNode(objNode)
XPath
Returns a String that represents the XPath for the XML mapping, which evaluates to the currently mapped XML node.
To set mapping for a content control, use the SetMapping method or the SetMappingByNode method. If the mapping is not active, using this property returns an error.
Dim objCC As ContentControl
Dim objMap As XMLMapping
Dim booMap As Boolean
Set objCC = ActiveDocument.ContentControls(1)
Set objMap = objCC.XMLMapping
If (objCC.Type = wdContentControlDate) And (objMap.XPath <> _
"/ns1:coreProperties[1]/ns0:createdate[1]") Then
booMap = objMap.SetMapping(XPath:="/ns1:coreProperties[1]/ns0:createdate[1]")
If booMap = False Then
MsgBox "Unable to map the content control."
End If
End If