0
votes

I want to show hide pictures in my PowerPoint Presentation using VB Macro. I wrote code that inserts some plots on a blank slide. For each figure, I create a Toggle Button. My idea is that when I click on the Button, the picture will be visible and, if I click again, it will disappear (all actions in Slide Show mode). The problem is that I cannot assign this function to the Toggle Button within the same routine (see below).

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

I know is it possible using a right click on the Toggle Button and choose a "View Code" option. I even managed to do that, using the following code:

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

But for me, this option is not interesting, once I have to create a subroutine for each Toggle Button, one by one.

I am working on Windows and I am using Powerpoint 2016.

Could someone help me?

Kind regards,

Murilo

1

1 Answers

0
votes

Assign each picture an action setting of Run Macro: ToggleVisibility then include something like this in your project:

Sub ToggleVisibility(oSh as Shape)
  oSh.Visible = Not oSh.Visible
End Sub