仅清除ContentControl内容VBA

问题描述 投票:0回答:1

在 Word 文档中,大约有 100 个具有适当标题和名称的内容控件 (CC)。但是,我只想清除多个 CC 的内容,保留 CC 本身。请仅注意内容。 DELETE和CLEAR函数删除CC,因此不能保留CC的内容。我要清除内容的CC标签是Section11、Section23、Section45、Section24、AppleID01。以下是我写的:

Sub ClearCCs_Contents()
  Dim CC As ContentControl
  For Each CC In ActiveDocument.ContentControls
      CC.Range.Text = ""
  Next CC
End Sub

我是否可以将“ActiveDocument.ContentControls”替换为上面提到的 CC 标签列表?或者还有其他直接简单的方法吗?

主要问题:使用内容控件的标签/标题名称创建内容控件列表,并运行循环以清除列表中项目上的 CC 内容。

excel vba automation ms-word
1个回答
0
投票
  • 使用
    Title
    Tag
    属性来识别所需的 CC
Sub ClearCCs_Contents()
  Dim CC As ContentControl, i As Long, arrCC
  Const CC_LIST = "Section11,Section23,Section45,Section24,AppleID01"
  arrCC = Split(UCase(CC_LIST), ",")
  For Each CC In ActiveDocument.ContentControls
    For i = LBound(arrCC) To UBound(arrCC)
        If UCase(CC.Title) = arrCC(i) Then
'        If UCase(CC.Tag) = arrCC(i) Then
            CC.Range.Text = ""
            Exit For
        End If
    Next
  Next CC
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.