PowerPoint VBA - 单击形状,然后按另一个形状更改颜色

时间:2013-12-10 20:19:39

标签: vba powerpoint

我是VBA的新手(从今天早上开始,所以请像对待白痴一样对待我,你会做对的!)并坚持看似应该非常简单的事情。

我在PowerPoint中工作,有一组圆圈,下面是红色和绿色方块。

我希望能够选择相关的圆圈,然后点击相应的方块,将该圆圈更改为红色或绿色,如下所示

 Select your option:   O         O          O              O             O


 Change colour:              [Red]                  [Green]

目前我正在使用动画和触发器,但我有很多圈子,我只想一次更改一个。

1 个答案:

答案 0 :(得分:0)

+1给Siddharth这个答案。如果你刚刚开始,还有一点不会显而易见。当形状触发宏时,你可以让PPT传递对点击形状的引用(注意:Mac PPT有缺陷/不完整。这在那里不起作用。)

使用Siddharth的建议作为起点,你可以这样做:

Option Explicit
Sub SelectMe(oSh As Shape)
' assign this macro to each of the shapes you want to color
' saves time to assign it to one shape, then copy the shape as many
' times as needed.
    ActivePresentation.Tags.Add "LastSelected", oSh.Name
End Sub
Sub ColorMeRed(oSh As Shape)
' assign this macro to the "color it red" shape

' when this runs because you clicked a shape assigned to run the macro,
' oSh will contain a reference to the shape you clicked

' oSh.Parent returns a reference to the slide that contains the shape
' oSh.Parent.Shapes(ActivePresentation.Tags("LastSelected")) returns a reference
' to the shape whose name is contained in the "LastSelected" tag,
' which was applied in the SelectMe macro above.
' Whew!

    If Len(ActivePresentation.Tags("LastSelected")) > 0 Then
        With oSh.Parent.Shapes(ActivePresentation.Tags("LastSelected"))
            .Fill.ForeColor.RGB = RGB(255, 0, 0)
        End With
    End If

End Sub
Sub ColorMeBlue(oSh As Shape)
' assign this macro to the "color it blue" shape
    If Len(ActivePresentation.Tags("LastSelected")) > 0 Then
        With oSh.Parent.Shapes(ActivePresentation.Tags("LastSelected"))
            .Fill.ForeColor.RGB = RGB(0, 0, 255)
        End With
    End If
End Sub