XMLMapping.SetMapping(String, String, CustomXMLPart) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Allows creating or changing the XML mapping on a content control. Returns True if Microsoft Office Word maps the content control to a custom XML node in the document’s custom XML data store.
public bool SetMapping (string XPath, string PrefixMapping = "", Microsoft.Office.Core.CustomXMLPart Source = default);
abstract member SetMapping : string * string * Microsoft.Office.Core.CustomXMLPart -> bool
Public Function SetMapping (XPath As String, Optional PrefixMapping As String = "", Optional Source As CustomXMLPart = null) As Boolean
Parameters
- 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.
- 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
- 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.
Returns
Boolean
Examples
The following example inserts a custom XML part and sets the XML for the custom part, and then inserts two content controls at the beginning of the document and maps the contents of the controls to the contents of XML elements in the custom part.
<span class="label">Dim objRange As Range
Dim objCustomPart As CustomXMLPart
Dim objCustomControl As ContentControl
Set objCustomPart = ActiveDocument.CustomXMLParts.Add
objCustomPart.LoadXML ("<books><book><author>Matt Hink</author>" & _
"<title>Migration Paths of the Red Breasted Robin</title>" & _
"<genre>non-fiction</genre><price>29.95</price>" & _
"<pub_date>2/1/2007</pub_date><abstract>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." & _
"</abstract></book></books>")
ActiveDocument.Range.InsertParagraphBefore
Set objRange = ActiveDocument.Paragraphs(1).Range
Set objCustomControl = ActiveDocument.ContentControls _
.Add(wdContentControlText, objRange)
objCustomControl.XMLMapping.</span>
<span class="label">SetMapping</span>
<span class="label">_ "/books/book/title", , objCustomPartobjRange.InsertParagraphAfterSet objRange = ActiveDocument.Paragraphs(2).RangeSet objCustomControl = ActiveDocument.ContentControls _ .Add(wdContentControlText, objRange)objCustomControl.XMLMapping.</span>
<span class="label">SetMapping</span>
<span class="label">_ "/books/book/abstract", , objCustomPart</span>
Remarks
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(CustomXMLNode) method.
Note: Creating a mapping for a rich-text content control causes a run-time error.