图像按钮图片更改

时间:2013-01-21 15:01:08

标签: imagebutton powerpoint-vba

如果用户激活图像按钮,则按钮的图片应该更改。我使用这段代码。

Dim mainslide As Object
Set mainslide = ActivePresentation.Slides(2)

If mainslide.Shapes(11).Name = "green" Then
    mainslide.Shapes(11).Picture = LoadPicture("pathname")
    fődia.Shapes(11).Name = "red"
Else
    mainslide.Shapes(11).Picture = LoadPicture("pathname2")
    fődia.Shapes(11).Name = "green"
End If

我得到运行时错误438,但在Excel中这种方法有效。

1 个答案:

答案 0 :(得分:0)

fodia未在您的示例中定义,因此很难猜测它应该是什么。但我不认为这是问题的根源。这是另一种有效的方法。有几点需要注意:

PPT对ActiveX对象的处理很奇怪。要获得用于控制对象的属性,必须使用Shape.OLEFormat.Object.property_name

至少在某些版本中,ActiveX形状不会以正常的形状顺序出现;您可以选中幻灯片上的形状以依次选择每个形状,但从不选择ActiveX形状。命名形状是一种解决方法(并且在重新排序形状时不会改变,因此它更可靠)。

Sub RedOrGreen()

Dim mainslide As Slide
Dim oSh As Shape
Set mainslide = ActivePresentation.Slides(1)

Set oSh = FindShapeNamed("green", mainslide)
If Not oSh Is Nothing Then
    oSh.OLEFormat.Object.Picture = LoadPicture("c:\temp\red.jpg")
    oSh.Name = "red"
    Exit Sub
End If

Set oSh = FindShapeNamed("red", mainslide)
If Not oSh Is Nothing Then
    oSh.OLEFormat.Object.Picture = LoadPicture("c:\temp\green.jpg")
    oSh.Name = "green"
    Exit Sub
End If

End Sub

Function FindShapeNamed(sName As String, oSl As Slide) As Shape
    Dim oSh As Shape
    For Each oSh In oSl.Shapes
        If oSh.Name = sName Then
            Set FindShapeNamed = oSh
            Exit Function
        End If
    Next
End Function