![如何隐藏所有表格样式](https://linux22.com/image/1506772/%E5%A6%82%E4%BD%95%E9%9A%90%E8%97%8F%E6%89%80%E6%9C%89%E8%A1%A8%E6%A0%BC%E6%A0%B7%E5%BC%8F.png)
我有一个 Word 2010 DOCX 文件,我想隐藏所有不适合我们的 CI/CD 的花哨表格样式。使用 UI 似乎无法做到这一点,所以我需要一个宏。
我尝试了以下
Sub Macro1()
Dim s As Style
For Each s In ActiveDocument.Styles
If s.Type = wdStyleTypeTable Then
If s.NameLocal <> "Table Grid" Then
Debug.Print (s.NameLocal)
s.Visibility = False
s.UnhideWhenUsed = False
Call s.Delete
End If
End If
Next
End Sub
它应该隐藏和删除除“表格网格”之外的所有表格样式,但仍然有太多可用的样式。
答案1
希望我已经正确理解了这个问题。有点晚了,但这段代码是从 Shauna Kelly 的示例开始创建的,并包含一个类似于您的测试,在我的 Word 2010 环境中运行得很好。不同之处在于您的代码中您想要隐藏的 .Visibility = False,但它应该是 True。我同意这没有意义,所以将 Word VBA 帮助中的相关行粘贴为注释。
图库中的表格样式仅显示我的自定义表格:
Sub HideTableStyleButMakeItVisibleWhenUsed()
'Hides all non-XX tables from the Gallery unless user manually finds and applies it.
Dim MyTableStyle As Style
For Each MyTableStyle In ActiveDocument.Styles
If MyTableStyle.Type = wdStyleTypeTable Then
If Left(MyTableStyle.NameLocal, 3) = "XX_" Then 'Remain visible in Gallery
With MyTableStyle
.Visibility = False ' True if the specified style is visible as a recommended style in the Styles gallery and in the Styles task pane. Read/write.
.UnhideWhenUsed = True
End With
Else 'Hide in Gallery
With MyTableStyle
.Visibility = True
.UnhideWhenUsed = True
End With
End If
End If
Next MyTableStyle
End Sub
答案2
这就是你想做的事吗?
隐藏表格样式但在使用时使其可见
Sub Hide_Table_Style()
With ActiveDocument.Styles(Word.wdStyleTableLightShading)
.Visibility = True ' Yes, True.
.UnhideWhenUsed = True
End With
End Sub
或者简单地隐藏表格样式
Sub Hide_Table_Style()
With ActiveDocument.Styles(Word.wdStyleTableLightShading)
.Visibility = True ' Yes, True.
.UnhideWhenUsed = False
End With
End Sub