【问题标题】:Is there any method or macro to remove all content controls in MS Word?是否有任何方法或宏可以删除 MS Word 中的所有内容控件?
【发布时间】:2019-03-28 22:49:12
【问题描述】:

我需要删除活动文档中的所有内容控制器(锁定和解锁)而不删除任何文本。我已经搜索并找到了一个宏。但我认为它不能正常工作。

Word VBA to delete Content Controls with specific Tags

可以这样做吗?

编辑: 我试过下面的代码。它将删除带有文本内容的内容控制器。我只需要删除内容控制器。

Sub Test()
Dim objCC As ContentControl
Do While ActiveDocument.ContentControls.Count > 0
For Each objCC In ActiveDocument.ContentControls
objCC.Delete True
Next
Loop
End Sub

【问题讨论】:

  • 它为什么不能正常工作?
  • 循环遍历 ActiveDocument.ContentControls 并删除每个项目。
  • @Cindy Meister:运行不正常意味着我在运行该宏代码后没有看到文档有任何变化。我尝试从本文档中删除内容控制器。 Download
  • @Florent B:谢谢,你能给我一个示例代码的答案吗?

标签: vba ms-word


【解决方案1】:

我能够在没有任何宏的情况下删除所有内容控件(在 Word 2013 中),如下所示:

  1. 在开发者菜单上,选择设计模式。
  2. 如果没有可见的内容控件,请滚动直到出现。
  3. 按 Ctrl+A 选择整个文档。
  4. 右键单击可见的内容控件。
  5. 在上下文菜单中,单击删除内容控件。

我不知道它是否适用于所有类型的内容控件。

【讨论】:

  • 以旧的 .doc 格式保存文档也会删除所有内容控件,只留下显示的内容。
  • 谢谢@macropod。我快疯了,想弄清楚为什么 Word 没有运行。
【解决方案2】:

我已经通过自己的方式找到了答案:)

Public Sub Test()

  Dim oRng As Range
  Dim CC   As ContentControl
  Dim LC   As Integer
  Dim LRCC As Integer
  Dim LTCC As Integer
  Dim LE   As Boolean

'Remove all content controls
Set oRng = ActiveDocument.Content
LTCC = LTCC + oRng.ContentControls.Count
For LC = oRng.ContentControls.Count To 1 Step -1

Set CC = oRng.ContentControls(LC)
If CC.LockContentControl = True Then
    CC.LockContentControl = False
End If
CC.Delete
If Not LE Then
    LRCC = LRCC + 1
    End If
    LE = False
Next
End Sub

这会对某人有所帮助。

【讨论】:

    【解决方案3】:

    感谢您的回答——这正是我想要的。我最终使用了以下内容:

    Sub ContentControlRemoval()
    '
    'Remove all content controls
    Set oRng = ActiveDocument.Content
    LTCC = LTCC + oRng.ContentControls.Count
    For LC = oRng.ContentControls.Count To 1 Step -1
    
    Set CC = oRng.ContentControls(LC)
    If CC.LockContentControl = True Then
        CC.LockContentControl = False
    End If
    CC.Delete
    If Not LE Then
        LRCC = LRCC + 1
        End If
        LE = False
    Next
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-05
      • 1970-01-01
      • 2013-08-22
      • 2011-03-02
      • 2010-09-18
      • 1970-01-01
      • 1970-01-01
      • 2014-11-28
      相关资源
      最近更新 更多