【问题标题】:Changing color on all but one table in Word with VBA使用 VBA 在 Word 中更改除一张以外的所有表格的颜色
【发布时间】:2013-01-08 16:56:31
【问题描述】:

我有一个宏,用于删除某些 Word 文档中所有表格中的所有颜色。被删除的颜色最初是为了指示某人应该在哪里输入。

相信我,我宁愿使用表单域或 ActiveX 文本框,但这不是它们可以工作的情况,因为 Word 是通过一个通过邮件合并使这些无效的第 3 方应用程序打开的。无论如何,我想跳过第一张桌子。我设置了下面的代码来执行此操作,然后将第一个表格的第一个单元格改回特定颜色。

Sub decolordocument()
'
' decolordocument Macro
'
'
Dim tbl As Table

For Each tbl In ActiveDocument.Tables
tbl.Shading.BackgroundPatternColor = wdColorWhite
Next
ActiveDocument.Tables(1).Cell(1, 1).Shading.BackgroundPatternColor = wdColorLightTurquoise

End Sub

这可以很好地去除颜色,但第一个表格的第一个单元格的颜色在所有表格中都不相同。我只想在 for each 循环期间跳过第一个表。我尝试了一个 if 语句(If tbl = ActiveDocument.Tables(1) Then...),但显然这不是允许的比较,因为它无法识别 Then 语句。我也试过用一个范围来做这个,但不能完全正确。任何想法将不胜感激。

【问题讨论】:

    标签: vba ms-word ms-office


    【解决方案1】:
    Sub decolordocument()
    '
    ' decolordocument Macro
    '
    '
    Dim first as Boolean
    Dim tbl As Table
    
    first = true
    
    For Each tbl In ActiveDocument.Tables
    If first Then
    first = false
    Else
    tbl.Shading.BackgroundPatternColor = wdColorWhite
    End If
    Next
    'ActiveDocument.Tables(1).Cell(1, 1).Shading.BackgroundPatternColor = wdColorLightTurquoise
    
    End Sub
    

    【讨论】:

    • 哦,哇...谈论废话!完美运行。感谢您的帮助。
    【解决方案2】:
        if activedocument.Tables.Count >1   then
    
          for x = 2 to activedocument.Tables.Count
    
         activedocument.Tables(x).Shading.BackgroundPatternColor = wdColorWhite
    
          next x
    
        end if
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多