制作组合框选择控件 Visibility/Non 形状 Powerpoint 的可见性 VBA
Making a ComboBox Selection Control Visibility/Non Visibility of Shapes Powerpoint VBA
我在幻灯片中有一个简单的组合框,其中添加了如下值:
0 = 2018 Pinot Noir
1 = 2019 Pinot Noir
2 = 2020 Pinot Noir
我现在想要用户选择来控制 Powerpoint 中的图像(形状)是否可见。所以我认为关于 ComboBox1_Change 事件的简单案例陈述就足够了。但是,我随后意识到我可能必须将上述值 分配 到幻灯片中的图像名称。这些名称与 ComboBox 值相同。
我以前做过这个,但我确信我缺少一个 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
我想控制的另一件事是,一旦用户完成选择,组合框的索引就会重置。
我觉得不能这样做有点愚蠢。一定是老了!如果可以请帮忙。
ComboBox1.Value 包含文本(即 2018 Pinot Noir),而不是列表索引。您还必须返回参考演示文稿以获取打开和关闭图像的代码。 imgPinot.Visible 仅当图像位于用户窗体上时才有效。
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
如果您的图片名称与您的组合框条目匹配,这应该有效
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
我在幻灯片中有一个简单的组合框,其中添加了如下值:
0 = 2018 Pinot Noir
1 = 2019 Pinot Noir
2 = 2020 Pinot Noir
我现在想要用户选择来控制 Powerpoint 中的图像(形状)是否可见。所以我认为关于 ComboBox1_Change 事件的简单案例陈述就足够了。但是,我随后意识到我可能必须将上述值 分配 到幻灯片中的图像名称。这些名称与 ComboBox 值相同。
我以前做过这个,但我确信我缺少一个 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
我想控制的另一件事是,一旦用户完成选择,组合框的索引就会重置。
我觉得不能这样做有点愚蠢。一定是老了!如果可以请帮忙。
ComboBox1.Value 包含文本(即 2018 Pinot Noir),而不是列表索引。您还必须返回参考演示文稿以获取打开和关闭图像的代码。 imgPinot.Visible 仅当图像位于用户窗体上时才有效。
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
如果您的图片名称与您的组合框条目匹配,这应该有效
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