我正在使用下面的宏来迭代Word文档,并从其中除第一个表之外的所有表中删除颜色。
由于使用合并字段自动填充这些文档的应用程序存在表单保护问题,我们的文档绝对不能与表单字段一起使用。因此,下一个最好的做法是在打印文档之前,只标记需要用阴影填充的内容,然后删除阴影。但是,当这段代码遍历文档时,它会删除某些表的一些边框。
我绝对不想这样,我只想去掉阴影。我不知道它为什么这样做,所以如果有人对此有任何见解,那将是最有帮助的。如果不是,如果我可以调整这个宏,使其只更改具有非白色背景颜色的单元格,那么也可以。
我尝试了嵌套循环的几个变体,但无法让它以这种方式运行。
Sub decolordocument()
Dim tbl As Table
Dim first As Boolean
first = True
For Each tbl In ActiveDocument.Tables
If first Then
first = False
Else
tbl.Shading.BackgroundPatternColor = wdColorWhite
End If
Next
MsgBox "Shaded cells in tables have been updated."
End Sub我也尝试过这段代码,得到了删除边框的相同效果:
Sub decolordocument()
Dim tbl As Table
Dim tblCount As Long
Dim i As Long
Dim first As Boolean
tblCount = ActiveDocument.Tables.Count
For i = 2 To tblCount
With ActiveDocument.Tables(i).Shading
.BackgroundPatternColor = wdColorWhite
End With
Next
MsgBox "Shaded cells in tables have been updated."
End Sub编辑:虽然我仍然看不出是什么原因使这些表格失去了边框,但我发现以某种方式拆分表格会使它们不会失去边框。我已经尽了最大的努力来隔离这个问题,但没有运气,因为似乎只有某些东西的组合才会导致边界的丢失。然而,至少我得到了一些可以工作的东西。如果有人可以提供一个宏来按照最初的请求遍历各个单元格,那么把它放在后面的口袋里可能是一个不错的选择。
发布于 2013-03-06 00:31:01
终于想出了一个宏用来遍历单元格。由于某些原因,我无法让嵌套的For each循环工作,直到我尝试了这个循环。所有带阴影的单元格都是相同的灰色阴影,所以我只比较了每个单元格,只有当它是灰色时才对其进行更改。这不是最有效的方法,但由于这些文档相当小,所以可以很好地工作。
Sub decolordocument()
Dim tbl As Table
Dim tblCount As Long
Dim cll As Word.Cell
Dim i As Long
tblCount = ActiveDocument.Tables.Count
For i = 2 To tblCount
For Each cll In ActiveDocument.Tables(i).Range.Cells
If cll.Shading.BackgroundPatternColor = RGB(217, 217, 217) Then
cll.Shading.BackgroundPatternColor = wdColorWhite
End If
Next
Next
MsgBox "Color in shaded cells has been removed."
End Sub发布于 2013-02-20 00:37:34
你必须使用.Shading来移除背景。
我将为一个文档演示它。请根据您的代码进行调整。
假设您的文档如下所示

试试这段代码
Option Explicit
Sub Sample()
Dim tblCount As Long
Dim i As Long
'~~> Get the Tables Count
tblCount = ActiveDocument.Tables.Count
'~~> If number of tables is less than 2 then exit sub
If tblCount < 2 Then Exit Sub
'~~> Start with 2nd table and loop
For i = 2 To tblCount
'~~> Remove background
With ActiveDocument.Tables(i).Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorAutomatic
End With
Next
End Sub这就是代码运行后文档的外观

https://stackoverflow.com/questions/14962214
复制相似问题