Word VBA 删除带有特定标签的内容控件 [英] Word VBA to delete Content Controls with specific Tags
问题描述
我在如何在 SO 上通过标签查找和禁用内容控件(以下是为方便起见的代码).就我而言,我需要删除带有特定标签的内容控件 (CC).
I read the answers to the question on How to find and disable a content control by tag on SO (below is the code for your convenience). In my case, I need to delete Content Controls (CC) with SPECIFIC tags.
例如,在文档中的 150 个 CC
中,我只需要查找和删除那些 CC
带有标签 "DCC"
(只是 CC,而不是其内容).如您所知,我在 VB 方面没有/有限的经验,非常感谢我可以复制/粘贴的脚本.
For Example, out of the 150 CCs
that I have in the document, I need to find and delete only those CCs
with the Tag "DCC"
(just the CC, not its contents). As you can tell, I have no/limited experience in VB and would greatly appreciate a script that I can copy/paste.
我在 Word 2007 中使用富文本抄送.
I'm using Rich Text CCs in Word 2007.
Private Sub DeleteCCByTag_Alternative(ccTag As String)
Dim cc As ContentControl
Set cc = ThisDocument.SelectContentControlsByTag(ccTag).Item(1)
With cc
.LockContentControl = False
.LockContents = False
.Range.Delete 'to delete CC content
.Delete (False)
End With
End Sub
推荐答案
好吧,我找到了一种方法来做到这一点......不是最漂亮的代码,我相信有更好的方法来做到这一点.但是,由于我对 VB 的了解极其有限,以下内容可以满足我的需求:
Well, I was able to figure out ONE way of doing this....not the prettiest code and I'm sure there's a better way to do this. But, with my extremely limited VB knowledge, the below is working for my needs:
Sub DeleteCCByTag()
Dim oThisdoc As Word.Document
Dim oCC As ContentControl
Dim oCCs As ContentControls
Set oThisdoc = ActiveDocument
Set oCCs = oThisdoc.SelectContentControlsByTag("DCC")
For Each oCC In oCCs
If oCCs.Count > 0 Then
oCC.Delete False
End If
Next
End Sub
这篇关于Word VBA 删除带有特定标签的内容控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!