1

我正在尝试创建一个宏,使用 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
4

2 回答 2

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
于 2016-08-18T12:24:04.390 回答
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 时出现内存不足错误,但这也发生在你的代码中,所以我希望它对你有用,如果你取消注释第二个最后一行。

于 2016-09-28T08:53:11.463 回答