2013-02-19 55 views
1

我正在使用以下宏遍历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 

编辑:虽然我还看不到具体是什么正在这些表失去他们的边界,我我们发现以某种方式分割表格会使他们不会失去边界。我已经尽了最大努力来隔离这个问题,因为似乎只有某些特定的组合会导致边界的损失。不过,至少我有一些工作。如果任何人都可以提供一个可以按照初始请求遍历各个单元格的宏,那么在后面的口袋里可能不会是一个不好的选择。

+0

我觉得你忘了在'下一步'之前的'结束如果'。请更正您的代码 – Saju 2013-02-19 16:23:28

+0

不,结束语句在那里。 – 2013-02-19 17:01:14

+0

你的错误已被@Siddharth Rout纠正,请点击“'编辑”后的链接查看更改:) – Saju 2013-02-19 17:07:11

回答

0

后最后的样子figu红出一个宏来遍历单元格。出于某种原因,我无法得到每个循环的嵌套工作,直到我尝试这个。所有阴影单元格都是相同的灰色阴影,所以我只比较了每个单元格,并且只在灰色时才更改它。不是最有效的方式,但由于这些文件相当小,这工作正常。

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 
1

您必须使用.Shading删除背景。

我将为它展示一个文档。请根据您的代码进行调整。

比方说,你的文件看起来像这样

enter image description here

试试这个代码

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 

这是您的文档的代码运行

enter image description here

+0

一个很好的建议,但这仍然会删除我的一些表格边框。再次,不知道为什么会发生。 – 2013-02-19 17:03:22

+0

你能分享你正在使用的代码吗?还要检查这些受影响的表格是否有边界。您可以通过手动删除背景来完成此操作;) – 2013-02-19 17:06:38

+0

您是否可以在www.wikisend.com上传受影响的文档并在此共享链接? – 2013-02-19 18:31:57

相关问题