【问题标题】:Word VBA to delete Content Controls with specific TagsWord VBA 删除具有特定标签的内容控件
【发布时间】:2016-03-24 09:35:50
【问题描述】:

我在 SO 上阅读了How to find and disable a content control by tag 上问题的答案(为方便起见,下面是代码)。就我而言,我需要删除带有特定标签的内容控件 (CC)。

例如,在文档中的 150 个 CCs 中,我只需要找到并删除带有标签 的那些 CCs >"DCC"(只是抄送,而不是其内容)。如您所知,我在 VB 方面没有/有限的经验,并且非常感谢我可以复制/粘贴的脚本。

我在 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

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    嗯,我能够找到一种方法来做到这一点....不是最漂亮的代码,我相信有更好的方法来做到这一点。但是,以我极其有限的 VB 知识,以下内容可以满足我的需求:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-02
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 2015-02-20
      相关资源
      最近更新 更多