将值存储在数组中并在VBA Powerpoint中显示

时间:2018-09-10 19:00:25

标签: arrays vba powerpoint powerpoint-vba

我正在尝试在PowerPoint幻灯片中检查红色字体。我想将包含红色字体的幻灯片编号存储在数组中,并在单个对话框中显示。当前,它在一个对话框中显示一个幻灯片编号。

我当前的代码如下所示。谁能告诉我如何存储它并显示它?

Private Sub CommandButton1_Click()

    Dim sld As Slide
    Dim shp As Shape
    Dim x As Byte

    With ActivePresentation
        z = .Slides(.Slides.Count).SlideNumber
        MsgBox z, vbDefaultButton1, "Total Slides"
    End With

    Dim myarray() As Integer
    ReDim myarray(0 To 2)

    For i = 2 To z
        Set sld = ActivePresentation.Slides(i)

        For Each shp In sld.Shapes
            If shp.TextFrame.TextRange.Font.Color.RGB = RGB(255, 0, 0) Then
                MsgBox i, vbDefaultButton2, "Slide with RED font"
            End If
        Next shp
    Next

End Sub

1 个答案:

答案 0 :(得分:1)

我会像这样使用collection代替数组

Private Sub CommandButton1_Click()

Dim sld As Slide
Dim shp As Shape
Dim x As Byte
Dim z, i

    With ActivePresentation
        z = .Slides(.Slides.Count).SlideNumber
        MsgBox z, vbDefaultButton1, "Total Slides"
    End With

    Dim myCol As Collection
    Set myCol = New Collection


    For i = 2 To z
        Set sld = ActivePresentation.Slides(i)
        For Each shp In sld.Shapes
            If shp.TextFrame.TextRange.Font.Color.RGB = RGB(255, 0, 0) Then
                ' MsgBox i, vbDefaultButton2, "Slide with RED font"
                myCol.Add CStr(i), CStr(i)
            End If
        Next shp
    Next

    Dim j As Long
    For j = 1 To myCol.Count
        Debug.Print myCol.Item(j)
    Next j


End Sub