Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

XMLMapping.SetMapping Method

Office 2007
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.


expression.SetMapping(XPath, PrefixMapping, Source)

expression   An expression that returns an XMLMapping object.


NameRequired/OptionalData TypeDescription
XPathRequiredStringSpecifies an XPath string that represents the XML node to which to map the content control. An invalid XPath string causes a run-time error.
PrefixMappingOptionalStringSpecifies 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.
SourceOptionalCustomXMLPartSpecifies 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.

Return Value


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.

Bb226172.vs_note(en-us,office.12).gif  Note
Creating a mapping for a rich-text content control causes a run-time error.


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.

Visual Basic for Applications
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." & _

Set objRange = ActiveDocument.Paragraphs(1).Range
Set objCustomControl = ActiveDocument.ContentControls _
    .Add(wdContentControlText, objRange)
objCustomControl.XMLMapping.SetMapping _
    "/books/book/title", , objCustomPart

Set objRange = ActiveDocument.Paragraphs(2).Range
Set objCustomControl = ActiveDocument.ContentControls _
    .Add(wdContentControlText, objRange)
objCustomControl.XMLMapping.SetMapping _
    "/books/book/abstract", , objCustomPart

Community Additions

© 2015 Microsoft