共用方式為


XMLMapping.SetMappingByNode 方法 (Word)

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

語法

expressionSetMappingByNode( _Node_ )

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

參數

名稱 必要/選用 資料類型 描述
節點 必要 CustomXMLNode 會指定要將目前內容控制項對應至其中的 XML 節點。

傳回值

布林值

註解

如果 XML 對應已經存在,Word 就會取代現有的 XML 對應,而且新對應 XML 節點的內容就會取代內容控制項的文字。 請參閱 SetMapping 方法。

注意事項

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

範例

下列範例會設定文件作者的內建文件屬性、將新的內容控制項插入使用中文件,然後將控制項的 XML 對應設定為內建文件屬性。

Dim objcc As ContentControl 
Dim objNode As CustomXMLNode 
Dim objMap As XMLMapping 
Dim blnMap As Boolean 
 
ActiveDocument.BuiltInDocumentProperties("Author").Value = "David Jaffe" 
 
Set objcc = ActiveDocument.ContentControls.Add _ 
 (wdContentControlDate, ActiveDocument.Paragraphs(1).Range) 
 
Set objNode = ActiveDocument.CustomXMLParts.SelectByNamespace _ 
 ("https://schemas.openxmlformats.org/package/2006/metadata/core-properties") _ 
 (1).DocumentElement.ChildNodes(1) 
 
Set objMap = objcc.XMLMapping 
blnMap = objMap.SetMappingByNode(objNode)

下列範例會建立自訂 XML 組件,然後建立兩個內容控制項,並將每個內容控制項對應至自訂 XML 中的特定節點。

Dim objRange As Range 
Dim objCustomPart As CustomXMLPart 
Dim objCustomControl As ContentControl 
Dim objCustomNode As CustomXMLNode 
 
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>2007-02-01</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 objCustomNode = objCustomPart.SelectSingleNode _ 
 ("/books/book/title") 
Set objCustomControl = ActiveDocument.ContentControls _ 
 .Add(wdContentControlText, objRange) 
objCustomControl.XMLMapping.SetMappingByNode objCustomNode 
 
objRange.InsertParagraphAfter 
Set objRange = ActiveDocument.Paragraphs(2).Range 
Set objCustomNode = objCustomPart.SelectSingleNode _ 
 ("/books/book/abstract") 
Set objCustomControl = ActiveDocument.ContentControls _ 
 .Add(wdContentControlText, objRange) 
objCustomControl.XMLMapping.SetMappingByNode objCustomNode

另請參閱

XMLMapping 物件

支援和意見反應

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