Share via


XMLMapping.SetMapping Method

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.

Namespace:  Microsoft.Office.Interop.Word
Assembly:  Microsoft.Office.Interop.Word (in Microsoft.Office.Interop.Word.dll)

Syntax

'Declaration
Function SetMapping ( _
    XPath As String, _
    PrefixMapping As String, _
    Source As CustomXMLPart _
) As Boolean
'Usage
Dim instance As XMLMapping
Dim XPath As String
Dim PrefixMapping As String
Dim Source As CustomXMLPart
Dim returnValue As Boolean

returnValue = instance.SetMapping(XPath, _
    PrefixMapping, Source)
bool SetMapping(
    string XPath,
    string PrefixMapping,
    CustomXMLPart Source
)

Parameters

  • XPath
    Type: System.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
    Type: System.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
    Type: 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.

Return Value

Type: System.Boolean
Boolean

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.

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.

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.SetMapping_    "/books/book/title", , objCustomPartobjRange.InsertParagraphAfterSet objRange = ActiveDocument.Paragraphs(2).RangeSet objCustomControl = ActiveDocument.ContentControls _    .Add(wdContentControlText, objRange)objCustomControl.XMLMapping.SetMapping_    "/books/book/abstract", , objCustomPart

See Also

Reference

XMLMapping Interface

XMLMapping Members

Microsoft.Office.Interop.Word Namespace