Word) (XMLMapping 物件
表示 ContentControl 物件上介於自訂 XML 與內容控制項之間的 XML 對應。 XML 對應是此文件之內容控制項中的文字與自訂 XML 資料存放區之 XML 元素之間的連結。
註解
您可以使用 SetMapping 方法來加入或變更使用 XPath 字串之內容控制項的 XML 對應。 下列範例會設定文件作者的內建文件屬性、將新的內容控制項插入使用中文件,然後將控制項的 XML 對應設定為內建文件屬性。
Dim objcc As ContentControl
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 objMap = objcc.XMLMapping
blnMap = objMap.SetMapping(XPath:="/ns1:coreProperties[1]/ns0:createdate[1]")
If blnMap = False Then
MsgBox "Unable to map the content control."
End If
使用 SetMappingByNode 方法來新增或變更內容控制項使用 CustomXMLNode 物件的 XML 對應。 下面範例同樣為上一個範例中,但是會使用 SetMappingByNode 方法。
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)
下面範例會建立新的 CustomXMLPart 物件、 載入自訂 XML 然後可以建立兩個新的內容控制項並將每個不同的 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>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 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
MsgBox objCustomControl.XMLMapping.IsMapped
請使用 Delete 方法來移除內容控制項的 XML 對應。 刪除內容控制項的 XML 對應只會刪除內容控制項與 XML 資料之間的連接。 內容控制項和 XML 資料都會保留在文件中。 下列範例會刪除使用中文件內目前對應之所有內容控制項的 XML 對應。
Dim objCC As ContentControl
For Each objCC In ActiveDocument.ContentControls
If objCC.XMLMapping.IsMapped Then
objCC.XMLMapping.Delete
End If
Next
請使用 IsMapped 屬性來判斷內容控制項是否對應至文件資料存放區中的 XML 節點。 下列範例會刪除使用中文件內所有已對應之內容控制項的 XML 對應。
Dim objCC As ContentControl
For Each objCC In ActiveDocument.ContentControls
If objCC.XMLMapping.IsMapped Then
objCC.XMLMapping.Delete
End If
Next
使用 CustomXMLNode 屬性來存取內容的控制項所對應的 XML 節點。 使用 CustomXMLPart 屬性來存取內容控制項對應的 XML 組件。 如需使用 CustomXMLNode 和 CustomXMLPart 物件的詳細資訊,請參閱各自物件主題。
請參閱
支援和意見反應
有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。