制作组合框选择控件形状的可见性/不可见性Powerpoint VBA



我在幻灯片中有一个简单的组合框,添加的值如下:

0 = 2018 Pinot Noir
1 = 2019 Pinot Noir
2 = 2020 Pinot Noir

我现在希望用户选择来控制Powerpoint中的图像(形状(是否可见。因此,我认为对ComboBox1_Change事件进行简单的case语句就足够了。然而,我后来意识到,我可能必须将上述值分配给幻灯片中图像的名称。这些名称与组合框值相同

我以前也这样做过,但我确信我错过了Powerpoint实现这一目标的一个对象。到目前为止,代码是:

Option Explicit
Private Sub ComboBox1_GotFocus()
If ComboBox1.ListCount = 0 Then AddDropDownItems
End Sub
Sub AddDropDownItems()
ComboBox1.AddItem "2018 Pinot Noir"
ComboBox1.AddItem "2019 Pinot Noir"
ComboBox1.AddItem "2020 Pinot Noir"
ComboBox1.ListRows = 3
'ComboBox1.Clear
End Sub
Sub ComboBox1_Change()
Dim imgPinot As Shape
Dim imgPinot2 As Shape
Dim imgPinot3 As Shape

Select Case ComboBox1.Value
Case 0
imgPinot.Visible = True
imgPinot2.Visible = False
imgPinot3.Visible = False
Case 1
imgPinot.Visible = False
imgPinot2.Visible = True
imgPinot3.Visible = False
Case 2
imgPinot.Visible = False
imgPinot2.Visible = False
imgPinot3.Visible = True
End Select
End Sub

我想控制的另一件事是,一旦用户完成选择,组合框的索引就会重置。

不能做这件事我觉得有点傻。一定是老了!如果可以,请提供帮助。

如果您的图像名称与组合框条目匹配,这应该会起作用

Option Explicit
Private Sub ComboBox1_GotFocus()
If ComboBox1.ListCount = 0 Then AddDropDownItems
End Sub
Sub AddDropDownItems()
ComboBox1.AddItem "2018 Pinot Noir"
ComboBox1.AddItem "2019 Pinot Noir"
ComboBox1.AddItem "2020 Pinot Noir"
ComboBox1.ListRows = 3
End Sub
Sub ComboBox1_Change()
Dim sel, n As Long, cb As ComboBox, nm

Set cb = Me.ComboBox1
sel = cb.Value                         'selected item
'loop over list 
For n = 1 To cb.ListCount
nm = cb.List(n - 1)                'list entry
Me.Shapes(nm).Visible = (nm = sel) 'show only if entry matches selection
Next n
End Sub

ComboBox1.值包含文本(即2018黑比诺(,而不是列表索引。您还必须参考演示文稿中打开和关闭图像的代码。imgPinot.Visible只有在图像位于UserForm上时才有效。

Private Sub ComboBox1_GotFocus()
If ComboBox1.ListCount = 0 Then AddDropDownItems
End Sub
Sub AddDropDownItems()
ComboBox1.AddItem "2018 Pinot Noir"
ComboBox1.AddItem "2019 Pinot Noir"
ComboBox1.AddItem "2020 Pinot Noir"
ComboBox1.ListRows = 3
End Sub
Private Sub ComboBox1_Change()
Dim imgPinot As Shape
Dim imgPinot2 As Shape
Dim imgPinot3 As Shape
Select Case ComboBox1.ListIndex
Case 0
With ActivePresentation.Slides(1)
.Shapes("imgPinot").Visible = True
.Shapes("imgPinot2").Visible = False
.Shapes("imgPinot3").Visible = False
End With
Case 1
With ActivePresentation.Slides(1)
.Shapes("imgPinot").Visible = False
.Shapes("imgPinot2").Visible = True
.Shapes("imgPinot3").Visible = False
End With
Case 2
With ActivePresentation.Slides(1)
.Shapes("imgPinot").Visible = False
.Shapes("imgPinot2").Visible = False
.Shapes("imgPinot3").Visible = True
End With
End Select
End Sub

最新更新