如何在发布者中设置所选单元格的背景颜色

时间:2016-08-17 20:15:23

标签: vba ms-publisher

我正在尝试创建一个宏,使用Publisher中的VBA将单元格中文本的字体颜色设置为白色,将单元格背景设置为黑色。

到目前为止,我已设法设置要更改的字体颜色,但我真的在背景中挣扎 - 我找不到合适的值来改变。

这是我到目前为止所拥有的:

Sub set_to_clue()

Selection.TextRange.Font.Color.RGB = RGB(255, 255, 255)
Selection.TextRange.Font.Fill.BackColor.RGB = RGB(0, 0, 0)

End Sub

进度 通过一些进一步的试验和错误,我已经找到了如何让细胞背景发生变化,但是目前我只能通过指定CellRange的项目编号来实现。这意味着更改颜色的单元格是硬编码而不是选定的单元格。如何计算料号?

Sub set_to_clue()

Selection.TextRange.Font.Color.RGB = RGB(255, 255, 255)
Selection.TableCellRange.Item(10).Fill.ForeColor.RGB = RGB(0, 255, 0)

End Sub

2 个答案:

答案 0 :(得分:0)

我现在有一个工作版本,但我确信这不是实现目标的正确或最优雅的方式。

它目前仅在单元格本身完全突出显示而不仅仅是其中的文本或仅仅是光标位于单元格中时才有效。我可能会稍后努力改进它。

Publisher 2016中的工作代码:

Sub invert_square()

For Each square In Selection.TableCellRange
    If square.Selected = True Then
        square.Fill.ForeColor.RGB = RGB(0, 0, 0)
        square.TextRange.Font.Color.RGB = RGB(255, 255, 255)
        Exit For
    End If
    Next

End Sub

答案 1 :(得分:0)

如果选择了整个表格(选择类型为pbSelectionShape和形状类型为pbTable),则扩展您的代码以使其适用于整个表格;如果选择类型为pbSelectionText,则扩展您的整个表格。

后一种功能的技巧是.ContainingObject引用整个Shape,并且每个Table Shape都包含一个Story对象。 TextRange对象的.Start和.End属性引用它在Story对象中的位置。通过比较这两个属性,我们能够识别所选文本属于哪个单元格(在Publisher中不可能同时在几个不同的单元格中选择一些文本)。

在我弄清楚这种方法之前,我尝试调用.Parent,直到TypeName()等于“Cell”,但这不起作用,因为Selection.TextRange的.Parent是Selection(而不是文档中的Parent)本身就像我希望的那样)

Option Explicit

Sub InvertSquare()
    ActiveDocument.BeginCustomUndoAction "Invert square"

    Dim oCell As Cell
    Dim oShape As Shape

    If selection.Type = pbSelectionTableCells Then
        Debug.Print "Table cells"

        For Each oCell In selection.TableCellRange
            SetInvertedColors oCell
        Next oCell

    ElseIf selection.Type = pbSelectionText Then
        Debug.Print "Text"

        Dim selText As TextRange
        Dim x As Variant

        Set selText = selection.TextRange
        Set x = selText.ContainingObject

        If TypeName(x) = "Shape" Then
            If x.Type = pbTable Then
                For Each oCell In x.Table.Cells
                    If oCell.HasText Then
                        If oCell.TextRange.Start <= selText.Start Then
                            If oCell.TextRange.End >= selText.End Then
                                SetInvertedColors oCell
                                Exit For
                            End If
                        End If
                    End If

                Next oCell
            End If
        End If

    ElseIf selection.Type = pbSelectionShape Then
        Debug.Print "ShapeRange"

        Dim oShapes As ShapeRange

        Set oShapes = selection.ShapeRange
        For Each oShape In oShapes
            If oShape.Type = pbTable Then
                For Each oCell In selection.TableCellRange
                    SetInvertedColors oCell
                Next oCell
                Exit For
            End If
        Next oShape
        Debug.Print "Shape"
    End If

    ActiveDocument.BeginCustomUndoAction "Invert square"
End Sub


Sub SetInvertedColors(oCell As Cell)
    Debug.Print oCell.TextRange.Text
    oCell.TextRange.Font.Color = RGB(255, 255, 255)
    ''oCell.Fill.ForeColor.RGB = RGB(0, 0, 0) ''Out of memory error for whatever reason
End Sub

出于某种原因,当我尝试在Publisher中设置.ForeColor.RGB时出现内存不足错误,但这也发生在我的代码中,所以我希望无论如何你都能使用它取消注释最后一行。

相关问题