问题描述
我想使用VB宏在PowerPoint演示文稿中显示隐藏图片。 我编写了在空白幻灯片上插入一些图的代码。 对于每个图,我创建一个切换按钮。 我的想法是,当我单击“按钮”时,图片将可见,如果再次单击,图片将消失(幻灯片显示模式下的所有操作)。 问题是我无法在同一例程中将此功能分配给“切换按钮”(请参见下文)。
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
我知道可以通过右键单击切换按钮并选择“查看代码”选项来实现。我什至使用以下代码设法做到了:
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
但是对于我来说,一旦我必须为每个“切换按钮”逐个创建一个子例程,这个选项就不会有趣了。
我正在使用Windows,并且正在使用Powerpoint 2016。
有人可以帮我吗?
亲切的问候,
Murilo
解决方法
为每张图片分配“运行宏”的操作设置:ToggleVisibility,然后在项目中包含以下内容:
with open(filename,"r") as file:
result = [line.split(',') for line in file]