首页 > 解决方案 > 如何使用vba宏从ms word文档中隐藏背景颜色为灰色的表格单元格

问题描述

我的 word doc 中有很多不同类型的表格。从那个灰色单元格不可用,所以我必须隐藏它(不想删除它)。表格的行数和列数总是不一样的。所以我不知道如何使用vba宏从ms word文档中隐藏背景颜色为灰色的表格单元格????

Sub ClearTableBGColor()
    Dim t As Table

    For Each t In ActiveDocument.Tables
    If t.Shading.BackgroundPatternColor = Grey Then
     t.Shading.BackgroundPatternColor.Hidden = True
        End If
    Next
End Sub

标签: vbams-word

解决方案


关于您将获得的最好的表格,如您的屏幕截图显示,与以下宏的作用类似。

Sub Demo()
Application.ScreenUpdating = False
Dim Tbl As Table, c As Long
With ActiveDocument
  For Each Tbl In .Tables
    With Tbl.Range
      For c = 1 To .Cells.Count
        With .Cells(c).Range
          If .Shading.BackgroundPatternColorIndex = wdGray25 Then
            With .ParagraphFormat
              .SpaceBefore = 0
              .SpaceAfter = 0
              .LineSpacingRule = wdLineSpaceExactly
              .LineSpacing = 0.7
            End With
            .Font.Hidden = True
          End If
        End With
      Next
    End With
  Next
End With
Application.ScreenUpdating = True
End Sub

请注意,您不能以这种方式完全隐藏一行。


推荐阅读