刪除內容控制項時從資料存放區移除資料
您可以藉由呼叫 ContentControl 物件的 Delete 方法來刪除內容控制項。 例如,下列程式碼會刪除具有 "MyTitle" 標題的內容控制項。
ActiveDocument.ContentControls.Item("MyTitle").Delete
您也可以藉由呼叫您想要移除之 CustomDataXMLNode 物件的 Delete 方法來刪除單一節點。 您可以藉由呼叫您想要移除之 CustomXMLPart 物件的 Delete 方法,刪除整個自訂 XML 組件。
如需內容控制項的詳細資訊,請參閱 使用內容控制項進行 Orking。 在這些範例中所使用的物件包括:
CustomXMLPart (Microsoft Office 系統核心物件模型)
(Microsoft Office 系統核心物件模型的CustomXMLParts)
範例 1
第一個程式碼範例會建立內容控制項,並在內容控制項上設定 XML 對應。
建立有效的自訂 XML 檔,並將它儲存到硬碟,然後將資料儲存區加入到文件中,其中包含您要對應的資訊。
假設此內容控制項會對應至下列的自訂 XML 檔案範例。
<?xml version="1.0" encoding="utf-8" ?>
<tree>
<fruit>
<fruitType>peach</fruitType>
<fruitType>pear</fruitType>
<fruitType>banana</fruitType>
</fruit>
</tree>
Now, suppose the content control is mapped to a <fruitType> node of the previous custom XML part.
Sub AddContentControlAndCustomXMLPart()
Dim strTitle As String
strTitle = "MyTitle"
Dim oContentControl As Word.ContentControl
Set oContentControl = ActiveDocument.ContentControls.Add(wdContentControlText)
oContentControl.Title = strTitle
ActiveDocument.CustomXMLParts.Add
ActiveDocument.CustomXMLParts(4).Load ("c:\mySampleCustomXMLFile.xml")
Dim strXPath As String
strXPath = "tree/fruit/fruitType"
oContentControl.XMLMapping.SetMapping strXPath
End Sub
範例 2
第二個程式碼範例會在刪除內容控制項時移除整個 CustomXMLPart 物件。
Private Sub Document_ContentControlBeforeDelete( _
ByVal OldContentControl As ContentControl, _
ByVal InUndoRedo As Boolean)
Dim objPart As CustomXMLPart
'Always void changing the Word document surface during undo!
If InUndoRedo Then
Return
End If
'Also delete the part with a root element called 'tree'
If OldContentControl.Title = "MyTitle" Then
For Each objPart In ActiveDocument.CustomXMLParts
If objPart.DocumentElement.BaseName = "tree" Then
objPart.Delete
End If
Next part
End If
End Sub
另請參閱
支援和意見反應
有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。