【问题标题】:Making a ComboBox Selection Control Visibility/Non Visibility of Shapes Powerpoint VBA制作组合框选择控制形状Powerpoint VBA的可见性/不可见性
【发布时间】:2020-10-11 02:36:26
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: vba combobox powerpoint shapes


    【解决方案1】:

    如果您的图像名称与您的组合框条目匹配,这应该可以工作

    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
    

    【讨论】:

    • 这非常有效,是循环图像的有效方式。
    【解决方案2】:

    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
    
    

    【讨论】:

    • 我尝试了您的代码,并确保它可以在表单上运行,但我不想使用它,因为我觉得 IF 在选择窗格下的 Powerpoint 中有一个手动控制打开或关闭图像/形状等,当然必须有一种方法可以在代码中执行相同的操作并将选择分配给 ComboBox ....我猜不是。也许我为我的演讲做的太多了。
    • 从您的 cmets 中,我推测您正在使用 ActiveX 控件,您在帖子中没有提及。同样的代码在控件中运行,我已经修改了我的列表,请再试一次。
    • 感谢您给我另一个解决方案。我已经和@TimWilliams 一起去了,但我一定会再次尝试你的。
    猜你喜欢
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 2019-11-09
    相关资源
    最近更新 更多