2012-02-13 115 views
0

我爲插件創建了一個代碼,其中我的所有代碼在單擊一個按鈕後一次運行。但是現在我想創建一個下拉列表,我可以選擇任何一個事件來運行。如何在PowerPoint 2007中使用VBA在插件中創建下拉列表?

下面是我對按鈕的代碼:

Sub Auto_Open() 
    Dim oToolbar As CommandBar 
    Dim oButton As CommandBarButton 
    Dim MyToolbar As String 

    ' Give the toolbar a name 
    MyToolbar = "Test" 

    On Error Resume Next 
    ' so that it doesn't stop on the next line if the toolbar's already there 

    ' Create the toolbar; PowerPoint will error if it already exists 
    Set oToolbar = CommandBars.Add(Name:=MyToolbar, _ 
     Position:=msoBarFloating, Temporary:=True) 
    If Err.Number <> 0 Then 
      ' The toolbar's already there, so we have nothing to do 
      Exit Sub 
    End If 

    On Error GoTo Errorhandler 

    ' Now add a button to the new toolbar 
    Set oButton = oToolbar.Controls.Add(Type:=msoControlButton) 

    ' And set some of the button's properties 
    With oButton 
     .DescriptionText = "Test_dist" 
      'Tooltip text when mouse if placed over button 
     .Caption = "Trial" 
     'Text if Text in Icon is chosen 
     .OnAction = "TEST" 
      'Runs the Sub Button1() code when clicked 
     .Style = msoButtonIconAndWrapCaptionBelow 
      ' Button displays as icon, not text or both 
     .FaceId = 1885 
     .TooltipText = "Test your presentation" 
    End With 

    ' You can set the toolbar position and visibility here if you like 
    ' By default, it'll be visible when created 
    oToolbar.Top = 50 
    oToolbar.Left = 150 
    oToolbar.Visible = True 

NormalExit: 

    Exit Sub ' so it doesn't go on to run the errorhandler code 

Errorhandler: 
    'Just in case there is an error 

    Resume NormalExit: 
End Sub 

回答

1

試試這個:

Dim oDropdown As CommandBarComboBox  
Set oDropdown = oToolbar.Controls.Add(Type:=msoControlDropdown) 
With oDropdown 
    .Clear 
    .AddItem ("This") 
    .AddItem ("That") 
    .AddItem ("The Other Thing") 
    ' default it to first item selected 
    .ListIndex = 1 
    .OnAction = "ProcessDropdown" 
End With 

而這種處理下拉的變化情況:

Sub ProcessDropdown() 
    MsgBox CommandBars("Test").Controls(2).Text 
End Sub 
+0

感謝史蒂夫Rindsberg – 2012-02-23 11:42:49

相關問題