Edit

Share via


XMLMapping.SetMapping(String, String, CustomXMLPart) Method

Definition

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 ("&lt;books&gt;&lt;book&gt;&lt;author&gt;Matt Hink&lt;/author&gt;" &amp; _































































































































    "&lt;title&gt;Migration Paths of the Red Breasted Robin&lt;/title&gt;" &amp; _































































































































    "&lt;genre&gt;non-fiction&lt;/genre&gt;&lt;price&gt;29.95&lt;/price&gt;" &amp; _































































































































    "&lt;pub_date&gt;2/1/2007&lt;/pub_date&gt;&lt;abstract&gt;You see them in " &amp; _































































































































    "the spring outside your windows.  You hear their lovely " &amp; _































































































































    "songs wafting in the warm spring air.  Now follow the path " &amp; _































































































































    "of the red breasted robin as it migrates to warmer climes " &amp; _































































































































    "in the fall, and then back to your back yard in the spring." &amp; _































































































































    "&lt;/abstract&gt;&lt;/book&gt;&lt;/books&gt;")































































































































































































































































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.

Applies to