VBA 关于CommandBar
您需要从命令栏释放焦点以修复错误。尝试将其添加到 ExecuteMso 行下方:
sld.Application.CommandBars.ReleaseFocus
除“ Button ”外,工具栏上的控件还包括“ ComboBox ”和“ PopUp ” 。
“工具栏”一词已消失。.. .. ..   从Office 2007 开始,采用了“功能区”而不是“工具栏”。
但是,在VBA上,“工具栏(CommandBar) ”仍然存在,并且在“工具栏(CommandBar) ”上运行的常规宏工作正常。工具栏本身显示为功能区上加载项选项卡的成员。
那么,在考虑“未来”如何移动到“功能区”时,仍然存在“功能区”不适合动态操作的问题。
这里说明的是使用常规的“工具栏”,但是由于常规的“工具栏”是存储在功能区的“加载项”选项卡中的,所以增加了对选择这个“加载项”选项卡的支持。我是。
但是,在VBA上,“工具栏(CommandBar) ”仍然存在,并且在“工具栏(CommandBar) ”上运行的常规宏工作正常。工具栏本身显示为功能区上加载项选项卡的成员。
那么,在考虑“未来”如何移动到“功能区”时,仍然存在“功能区”不适合动态操作的问题。
这里说明的是使用常规的“工具栏”,但是由于常规的“工具栏”是存储在功能区的“加载项”选项卡中的,所以增加了对选择这个“加载项”选项卡的支持。我是。
这是在上一页的“ 3 个按钮”示例中添加了ComboBox和PopUp的示例。(部分展示)
在上一页源代码示例的⑦之前添加了以下主要部分。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | '***************************************************************************************************Private Sub Auto_Open()    '-----------------------------------------------------------------------------------------------    Dim objBar As CommandBar                                        ' CommandBar    Dim objCont As CommandBarControl                                ' CommandBarControl    Dim objCombo As CommandBarComboBox                              ' CommandBarComboBox    Dim objPopUp As CommandBarPopup                                 ' CommandBarPopup    Dim intIx As Integer                                            Dim intIxT As Integer                                             Dim intY As Integer                                               Dim intM As Integer                                               Dim blnTrue As Boolean                                           Dim tblYM(11) As String                                           intY = Year(Date)    intM = 4    If Month(Date) < 4 Then intY = intY - 1    For intIx = 0 To 11        tblYM(intIx) = CStr(intY) & "年"& Format(intM, "00") & "月"        If intY = Year(Date) And intM = Month(Date) Then intIxT = intIx        intM = intM + 1        If intM > 12 Then            intY = intY + 1            intM = 1        End If    Next intIx    '-----------------------------------------------------------------------------------------------    ' 年月ComboBox    Set objCont = objBar.Controls.Add(Type:=msoControlComboBox)      objCont.BeginGroup = True    Set objCombo = objCont    With objCombo                                                         .Style = msoComboLabel        .Width = 120                     .Caption = "年月"        For intIx = 0 To 11            .AddItem tblYM(intIx)        Next intIx        .ListIndex = intIxT        .OnAction = "CBO_Click"    End With    '-----------------------------------------------------------------------------------------------    ' PopUp    Set objCont = objBar.Controls.Add(Type:=msoControlPopup)         objCont.BeginGroup = True    Set objPopUp = objCont    objPopUp.Caption = "サブメニュー"    blnTrue = False    For intIx = 0 To 2                                                   Set objCont = objPopUp.Controls.Add(Type:=msoControlButton)        objCont.BeginGroup = blnTrue        Set objBtn = objCont        objBtn.Style = msoButtonCaption                objBtn.Caption = vntCaption2(intIx)          objBtn.TooltipText = vntTipText(intIx)         objBtn.OnAction = vntOnAction(intIx)           blnTrue = True    Next intIxEnd Sub | 
①将组合添加到工具栏。只有“类型”与添加按钮的情况不同。
②将该项目添加到组合列表中。它与UserForm ComboBox控件完全相同。
③向工具栏添加一个弹出窗口。
④在弹出窗口中添加三个按钮。这个过程与在上一个工具栏中添加3个按钮完全相同,只是要添加( Add)的人⑤从“objBar”变为“ objPopUp ”。
对于通过宏添加的工具栏,“加载项”选项卡将出现在功能区上,并且会像这样显示。
默认情况下,“加载项”选项卡处于选中状态。
单击按钮或组合时执行的过程是在OnAction中注册的过程。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | '***************************************************************************************************Private Sub BTN_TOUROKU()    MsgBox "登録"End Sub'***************************************************************************************************Private Sub BTN_KOUSHIN()    '-----------------------------------------------------------------------------------------------    MsgBox "「更新」"End Sub'***************************************************************************************************Private Sub BTN_SAKUJO()    '-----------------------------------------------------------------------------------------------    MsgBox "「削除」"End Sub'***************************************************************************************************Private Sub CBO_Click()    '-----------------------------------------------------------------------------------------------    Dim xlAPP As Application                                        ' Excel.Application    Dim objBar As CommandBar                                        ' CommandBar    Dim objCont As CommandBarControl                                ' CommandBarControl    Dim objCombo As CommandBarComboBox                              ' CommandBarComboBox    Set xlAPP = Application    Set objBar = xlAPP.CommandBars(g_cnsTitle)    Set objCombo = objBar.Controls(4)    MsgBox "「コンボ」" | 
在工具栏上注册按钮或组合时,这些过程中的每一个都在OnAction属性中设置。
对于按钮,每个按钮调用一个单独的过程,但对于组合,单击列表中的任何项目都会调用相同的过程。因此,有必要确定被调用过程选择了哪个项目。这是通过获取组合对象(工具栏上的第四个控件)然后通过其选择索引 ( ListIndex ) 或选定文本(Text)来确定的。
在此示例中,工具栏的名称是固定的,但工具栏名称在应用程序级别是“Alternative”。换句话说,如果您打开多个实现相同宏的工作簿,工具栏将已经存在于您稍后打开的工作簿中,从而导致运行时错误。
为避免这种情况,您必须动态控制工具栏的名称。
例如,可以使用不带扩展名的工作簿名称,但如果使用名称保存,之后工作簿名称会发生变化,因此打开时工具栏的名称保存在隐藏单元格中。
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号