首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用切换按钮Powerpoint VBA显示/隐藏图片

使用切换按钮Powerpoint VBA显示/隐藏图片
EN

Stack Overflow用户
提问于 2020-09-09 21:02:34
回答 1查看 791关注 0票数 0

我想使用VB宏在PowerPoint演示文稿中显示隐藏图片。我写了在空白幻灯片上插入一些绘图的代码。对于每个图形,我创建了一个切换按钮。我的想法是,当我点击按钮时,图片将是可见的,如果我再次点击,它将消失(幻灯片放映模式下的所有操作)。问题是我不能将这个函数分配给同一例程中的Toggle Button (如下所示)。

代码语言:javascript
运行
复制
Sub insertPics()

Dim strFolder As String ' Full path to folder
Dim strName As String
Dim oPres As Presentation
Dim osld As Slide
Dim ocust As CustomLayout
Dim vertPos As Long
Dim plotNumb As Long
Dim plotFig As Shape
Dim toggBut As Shape

'Delete all shapes in the slide
For Each Sld In ActivePresentation.Slides
    TotalShapes = Sld.Shapes.Count
    For i = TotalShapes To 1 Step -1
        Sld.Shapes(i).Delete
    Next
Next

' Folder where pictures are located:
strFolder = "C:\Users\MyUser\pictures\"

Set oPres = ActivePresentation
Set osld = oPres.Slides(oPres.Slides.Count)
Set ocust = osld.CustomLayout

strName = Dir$(strFolder & "*.bmp")
vertPos = 100
While strName <> ""

plotNumb = plotNumb + 1
vertPos = vertPos + 37

Set plotFig = osld.Shapes.AddPicture(strFolder & strName, msoFalse, msoTrue, Left:=150, Top:=120, Width:=525, Height:=297)
    With plotFig
        .Line.Visible = True
        .Line.ForeColor.RGB = vbWhite
        If plotNumb = 1 Then
            .Name = "AxisSystem"
            .Visible = True
        Else
            .Name = "Plot" & (plotNumb - 1)
            .Visible = False
        End If
        With .PictureFormat
            ColorToChange = RGB(255, 255, 255)
            .TransparentBackground = msoTrue
            .TransparencyColor = ColorToChange
        End With
        .Fill.Visible = msoFalse
    End With

If plotNumb > 1 Then
    Set toggBut = Application.ActiveWindow.View.Slide.Shapes.AddOLEObject(ClassName:="Forms.ToggleButton.1", Link:=True)
            With toggBut.OLEFormat.Object
                .Top = vertPos
                .Left = 750
                .Height = 30
                .Width = 50
                .Caption = "Plot " & (plotNumb - 1)
                .BackStyle = 0
                .Name = "TB" & (plotNumb - 1)
                With .Font
                    .Size = 10
                End With
            End With
End If

strName = Dir()

Wend

End Sub

我知道用鼠标右键点击切换按钮并选择“查看代码”选项是可能的。我甚至设法做到了这一点,使用了以下代码:

代码语言:javascript
运行
复制
Private Sub TB1_Click()
If TB1.Value = True Then
    ActivePresentation.Slides(1).Shapes("Plot1").Visible = True
Else
    ActivePresentation.Slides(1).Shapes("Plot1").Visible = False
End If
End Sub


Private Sub TB2_Click()
If TB2.Value = True Then
    ActivePresentation.Slides(1).Shapes("Plot2").Visible = True
Else
    ActivePresentation.Slides(1).Shapes("Plot2").Visible = False
End If
End Sub


Private Sub TB3_Click()
If TB3.Value = True Then
    ActivePresentation.Slides(1).Shapes("Plot3").Visible = True
Else
    ActivePresentation.Slides(1).Shapes("Plot3").Visible = False
End If
End Sub

但对我来说,这个选项并不有趣,一旦我必须为每个Toggle Button逐个创建子例程。

我正在Windows上工作,正在使用Powerpoint 2016。

有人能帮帮我吗?

致以亲切的问候,

穆里洛

EN

回答 1

Stack Overflow用户

发布于 2020-09-12 09:58:29

为每个图片分配一个Run Macro: ToggleVisibility的操作设置,然后在您的项目中包含类似以下内容:

代码语言:javascript
运行
复制
Sub ToggleVisibility(oSh as Shape)
  oSh.Visible = Not oSh.Visible
End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63812301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档