【问题标题】:How to change table border color in PPT VBA如何在 PPT VBA 中更改表格边框颜色
【发布时间】:2020-06-14 23:47:07
【问题描述】:

我正在尝试根据边框是否为某种颜色来更改每张幻灯片上表格边框的颜色。但是,我不断收到运行时错误 91 - 对象变量或未设置块变量。知道我哪里出错了吗?

Dim myTable As Table
Dim sh As Shape
Dim sl As Slide
Dim iRow As Integer
Dim iCol As Integer

For Each sl In ActivePresentation.Slides
    For Each sh In sl.Shapes
        If sh.HasTable Then Set myTable = sh.Table
        For iRow = 1 To myTable.Rows.count
            For iCol = 1 To myTable.Columns.count
                If myTable.Cell(iRow, iCol).Borders(ppBorderTop).ForeColor.RGB = RGB(0, 0, 0) Then myTable.Cell(iRow, iCol).Borders(ppBorderTop).ForeColor.RGB = RGB(100, 0, 0)
                If myTable.Cell(iRow, iCol).Borders(ppBorderBottom).ForeColor.RGB = RGB(0, 0, 0) Then myTable.Cell(iRow, iCol).Borders(ppBorderBottom).ForeColor.RGB = RGB(100, 0, 0)
                If myTable.Cell(iRow, iCol).Borders(ppBorderLeft).ForeColor.RGB = RGB(0, 0, 0) Then myTable.Cell(iRow, iCol).Borders(ppBorderLeft).ForeColor.RGB = RGB(100, 0, 0)
                If myTable.Cell(iRow, iCol).Borders(ppBorderRight).ForeColor.RGB = RGB(0, 0, 0) Then myTable.Cell(iRow, iCol).Borders(ppBorderRight).ForeColor.RGB = RGB(100, 0, 0)
            Next iCol
        Next iRow
    Next sh
Next sl

【问题讨论】:

  • 你看过这个答案VBA: Set border for Powerpoint table 吗?
  • @PeterT 谢谢。我已经创建了一些代码,但是我得到了运行时错误 91(更新的 Q)有什么想法吗?
  • 您在表格上的整个双循环需要在您的 If sh.HasTable Then 语句中。您可能会在没有表格的幻灯片上遇到错误。
  • ^ 需要使用 If...Then...End If 的多行语法。
  • 有了这样的所有功能,试着录制一个宏,看看它是如何设置属性的,然后将它应用到你自己的代码中。

标签: vba powerpoint


【解决方案1】:

由于在 VBA 中您无法直接比较 RGB,因此您需要将其转换为数字,如下面的自我解释且易于理解的代码。

Option Explicit

Sub test()
    Dim myTable As Table
    Dim sh As Shape
    Dim sl As Slide
    Dim iRow As Integer
    Dim iCol As Integer

    'set a variable to convert from RGB to Double
    Dim lngvar As Double

    'set the color check
    lngvar = RGB(0, 0, 0)

    Dim o As New Collection
    Set o = getRGB(lngvar)

    'assign the new color
    lngvar = RGB(98, 117, 182)

    Dim c As New Collection
    Set c = getRGB(lngvar)

    For Each sl In ActivePresentation.Slides
        For Each sh In sl.Shapes
            If sh.HasTable Then Set myTable = sh.Table
                For iRow = 1 To myTable.rows.Count
                    For iCol = 1 To myTable.Columns.Count
                        If myTable.cell(iRow, iCol).Borders(ppBorderTop).ForeColor.RGB = RGB(o("R"), o("G"), o("B")) Then myTable.cell(iRow, iCol).Borders(ppBorderTop).ForeColor.RGB = RGB(c("R"), c("G"), c("B"))
                        If myTable.cell(iRow, iCol).Borders(ppBorderBottom).ForeColor.RGB = RGB(o("R"), o("G"), o("B")) Then myTable.cell(iRow, iCol).Borders(ppBorderBottom).ForeColor.RGB = RGB(c("R"), c("G"), c("B"))
                        If myTable.cell(iRow, iCol).Borders(ppBorderLeft).ForeColor.RGB = RGB(o("R"), o("G"), o("B")) Then myTable.cell(iRow, iCol).Borders(ppBorderLeft).ForeColor.RGB = RGB(c("R"), c("G"), c("B"))
                        If myTable.cell(iRow, iCol).Borders(ppBorderRight).ForeColor.RGB = RGB(o("R"), o("G"), o("B")) Then myTable.cell(iRow, iCol).Borders(ppBorderRight).ForeColor.RGB = RGB(c("R"), c("G"), c("B"))
                    Next iCol
                Next iRow
            Next sh
        Exit For
    Next sl
End Sub

Function getRGB(lngCol As Double) As Collection
    Set getRGB = New Collection
    Dim iR As Integer
    Dim iG As Integer
    Dim iB As Integer

    iR = lngCol Mod 256
    iG = (lngCol \ 256) Mod 256
    iB = (lngCol \ 256 \ 256) Mod 256

    Dim item As Variant
    Dim key As String
    key = "R"
    item = iR
    getRGB.Add item, key

    key = "G"
    item = iG
    getRGB.Add item, key

    key = "B"
    item = iB
    getRGB.Add item, key

End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 2013-06-21
    • 2015-02-06
    • 1970-01-01
    相关资源
    最近更新 更多