共用方式為


Word) (XMLMapping.SetMapping 方法

允許建立或變更內容的控制項上的 XML 對應。 如果 Microsoft Word 將內容控制項對應至檔自訂 XML 資料存放區中的自訂 XML 節點,則會傳回 True

語法

expressionSetMapping( _XPath_ , _PrefixMapping_ , _Source_ )

表達 會傳回 'XMLMapping' 物件的運算式。

參數

名稱 必要/選用 資料類型 描述
XPath 必要 String 會指定 XPath 字串,表示要將內容控制項對應至其中的 XML 節點。 無效的 XPath 字串會導致執行階段錯誤。
PrefixMapping 選用 String 會指定查詢 XPath 參數中提供的運算式時,要使用的前置詞對應。 如果省略此參數,Word 就會使用目前文件中指定自訂 XML 組件的前置詞對應集。
Source 選用 CustomXMLPart 指定要將內容控制項對應至所需的自訂 XML 資料。 如果省略此參數,XPath 評估對目前的文件中的所有自訂 XML 與建立對應於順序 xpath,就會解析為 XML 節點的第一個 CustomXMLPart

傳回值

布林值

註解

如果 XML 對應已經存在,Word 就會取代現有的 XML 對應,而且新對應之 XML 節點的內容就會取代內容控制項的文字。 如果指定的 XPath 並未評估成指定之自訂 XML 組件中的 XML 節點,您仍然可以指定此對應,而且將會建立一個對應。 當指定之 XPath 評估成指定之自訂 XML 組件中的 XML 節點時,這個對應就會自動連結。

請參閱 SetMappingByNode 方法。

注意事項

建立 RTF 內容控制項的對應將會產生執行階段錯誤。

範例

下列範例會插入自訂 XML 組件並設定自訂組件的 XML,然後在文件的開頭插入兩個內容控制項,並將這些控制項的內容對應至自訂組件中 XML 元素的內容。

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", , objCustomPart 
 
objRange.InsertParagraphAfter 
Set objRange = ActiveDocument.Paragraphs(2).Range 
Set objCustomControl = ActiveDocument.ContentControls _ 
 .Add(wdContentControlText, objRange) 
objCustomControl.XMLMapping.SetMapping _ 
 "/books/book/abstract", , objCustomPart

另請參閱

XMLMapping 物件

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應