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



我是VBA的新手(从今天早上开始,所以请把我当作白痴对待,你会做对的!)我陷入了一件似乎应该很简单的事情。

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

我希望能够选择一个相关的圆圈,然后点击适当的正方形,将该圆圈仅更改为红色或绿色,如下所示

 Select your option:   O         O          O              O             O

 Change colour:              [Red]                  [Green]

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

+1给悉达思。如果你刚开始的话,还有一点不会很明显。当点击的形状触发宏时,你可以让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

最新更新