《Excel的工具栏和菜单栏VBA源码实例.doc》由会员分享,可在线阅读,更多相关《Excel的工具栏和菜单栏VBA源码实例.doc(8页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、. -工具栏和菜单栏工具栏和菜单栏的运用更多时候是伴随着加载宏和个性Excel界面的出现而出现。在不断加深对Excel VBA的理解和运用,我们编程的思路渐渐会转到考虑代码的通用性和应用方案上,将代码和Excel数据源分开。因此,制作更多具有通用功能的加载宏不管是xla加载宏,还是加载宏,可以最大极限的发挥VBA编程的魅力,而不是要求用户强制启用宏。也正是因为这个原因,在我们去学习工具栏和菜单栏时,要明白的一个道理是,制作工具栏仅仅是为了加载宏等具体运用的实现,不要一味地去追求工具栏的花哨。一、几个根本概念在开场本节之前,先理解什么是命令栏?命令栏mandBars:是工具栏、菜单栏和快捷菜单的
2、统称。工具栏:带有按钮和选项的工具条,使用这些按钮和选项可执行命令。如下列图:菜单栏:标题栏下的水平栏,包括菜单名称。如下列图快捷菜单:又叫弹出式菜单,鼠标右键单击。如下列图。二、mandBars集合对象通过上面几幅图片的直观概念之后,我们接下来理解mandBar集合。所有的工具栏和菜单栏代码都是围绕mandbars集合展开的。mandBarControls集合包含三种类型控件。mandBarButton:代表命令栏中的一个按钮控件按钮控件:工具栏上的按钮,或菜单、子菜单或快捷菜单上的菜单项,当单击它们时会运行一条命令。工具栏按钮和菜单项共享一样的属性和方法。该控件的 Type 属性必须是 m
3、soControlButton。mandBarboBox:代表命令栏中的一个组合框控件组合框控件:菜单栏、工具栏、菜单、子菜单或快捷菜单上的自定义编辑框、下拉列表框或组合框。当工具栏垂直停靠时,它所包含的任何自定义组合框控件都不可见。该控件的 Type 属性必须是 msoControlEdit、msoControlDropdown、msoControlboBox、msoControlButtonDropdown、msoControlSplitDropdown、msoControlOCXDropdown、msoControlGraphicbo 或 msoControlGraphicDropdow
4、n。mandBarPopup:代表命令栏中的一个弹出式控件弹出式控件:是菜单栏或工具栏上的置或自定义控件,当单击它时显示菜单,或者是菜单、子菜单、或快捷菜单上的置或自定义菜单项,当指针放在其上时显示子菜单。该控件的 Type 属性必须是 msoControlPopup、msoControlGraphicPopup、msoControlButtonPopup、msoControlSplitButtonPopup 或 msoControlSplitButtonMRUPopup。几种常见属性,参数和方法:VisibleNameTypePostionTemporaryCaptionOnActionFa
5、ceIDStyleEnableTop/Left/Width/HightBeginGroupControlsAdd方法Findcontrols方法下面将通过实例来解释上述属性、参数和方法的运用。三、实例代码1、 建立一命令栏Application.mandBars.Add即建立了一个工具栏。一般的,我们会相应的定义一个mandbar对象来操作这个自定义工具栏,如下代码:Sub AddmandBar1() 添加一自定义工具栏 Dim cmdBar As mandBar Set cmdBar = Application.mandBars.AddEnd Sub但,Excel好似任何变化,这是因为自定义
6、工具栏的默认Visible为False。Sub AddmandBar2() 添加一自定义工具栏,并显示 Dim cmdBar As mandBar Set cmdBar = Application.mandBars.Add cmdBar.Visible = TrueEnd Sub2、 Position例如Position: 默认值为 msoBarFloating常量 说明msoBarLeft、msoBarTop、msoBarRight 和 msoBarBottom 指定新命令栏的左侧、顶部、右侧和底部坐标msoBarFloating 指定新命令栏不固定msoBarPopup 指定新命令栏为快捷
7、菜单msoBarMenuBar 仅适用于 Macintosh 机Sub AddmandBar3() Dim cmdBar As mandBar Set cmdBar = Application.mandBars.Add(, , , Temporary:=True) With cmdBar .Name = My Bar .Visible = True .Position = msoBarTop End WithEnd SubSub AddmandBar4() Dim cmdBar As mandBar Set cmdBar = Application.mandBars.Add(Name:=My
8、Bar, Position:=msoBarTop, Temporary:=True) cmdBar.Visible = TrueEnd Sub为了防止出现重复的自定义工具栏,常规的代码写法是先删除工具栏后,再添加。Sub AddmandBar5() Dim cmdBar As mandBar Call DeletemandBar Set cmdBar = Application.mandBars.Add(Name:=My Bar, Position:=msoBarTop, Temporary:=True) cmdBar.Visible = TrueEnd SubSub DeletemandBa
9、r() On Error Resume Next Application.mandBars(My Bar).DeleteEnd Sub3、 mandBar Controls Type例如接下来我们介绍mandBarControl对象mandBarControl对象与 mandBarButton、mandBarboBox 以及 mandBarPopup 对象具有同样的属性和方法.Sub AddCmdCtlType() Dim cmdBar As mandBar Dim cmdBtn As mandBarButton Dim cmdbo As mandBarboBox Dim cmdPop As
10、mandBarPopup Call DeleteCtl Set cmdBar = Application.mandBars.Add(Name:=mandControl Type, Temporary:=True) With cmdBar .Visible = True Set cmdBtn = .Controls.Add(Type:=msoControlButton) With cmdBtn .Caption = Button .Style = msoButtonCaption End With Set cmdPop = .Controls.Add(Type:=msoControlPopup)
11、 With cmdPop .Caption = Popup End With Set cmdbo = .Controls.Add(Type:=msoControlboBox) With cmdbo .Caption = bo End With End WithEnd SubSub DeleteCtl() On Error Resume Next Application.mandBars(mandControl Type).DeleteEnd Sub4、 Width、Height例如Sub AddButtonHight() Dim cmdBar As mandBar Dim cmdBtn As
12、mandBarButton Call DeleteBtn Set cmdBar = Application.mandBars.Add(Name:=cmdBtn Type, temporary:=True) With cmdBar .Visible = True Set cmdBtn = .Controls.Add(Type:=msoControlButton) With cmdBtn .Caption = Hight Show .Style = msoButtonCaption .Height = 50 End With End WithEnd SubSub DeleteBtn() On Er
13、ror Resume Next Application.mandBars(cmdBtn Type).Delete On Error GoTo 0End Sub5、 置FaceID、OnAction和Style在mandBarButton中的例如Sub AddCmdButton() Dim cmdBar As mandBar Dim cmdBtn As mandBarButton Dim cmdBtn2 As mandBarButton Call DeleteBtn Set cmdBar = Application.mandBars.Add(Name:=cmdBtn Type, Temporar
14、y:=True) With cmdBar .Visible = True Set cmdBtn = .Controls.Add(Type:=msoControlButton) With cmdBtn .Caption = Button1 .FaceId = 12 .OnAction = ButtonShow1 .Style = msoButtonIconAndCaption End With Set cmdBtn2 = .Controls.Add(Type:=msoControlButton) With cmdBtn2 .Caption = Button2 .FaceId = 13 .OnAc
15、tion = ButtonShow2 .Style = msoButtonIconAndCaption End With End WithEnd SubSub ButtonShow1() MsgBox Button1 testEnd SubSub ButtonShow2() MsgBox Button2 testEnd Sub6、 利用个性图案制作自己的FaceID参考:AddCustomICO.xls7、 mandBar Type例如:Popup参考:mandBar Popup1.xls、mandBar Popup2.xls、mandBar Popup Form.xls8、 建立一菜单栏Su
16、b AddMenuBar() Dim cmdBar As mandBar Dim cmdMenu As mandBarPopup Dim cmdBtn As mandBarButton Call DeleteMenuBar Set cmdBar = Application.mandBars(WorkSheet Menu Bar) Set cmdBar = Application.mandBars(1) With cmdBar Set cmdMenu = .Controls.Add(Type:=msoControlPopup, temporary:=True) With cmdMenu .Cap
17、tion = My Menu Set cmdBtn = .Controls.Add(Type:=msoControlButton) With cmdBtn .Caption = Item1 .OnAction = Item1Action .FaceId = 12 End With End With End WithEnd SubSub Item1Action() MsgBox Menu Item testEnd SubSub DeleteMenuBar() On Error Resume Next Application.mandBars(1).Controls(My Menu).Delete
18、End Sub9、 利用置命令制作自己的菜单Sub AddMenuBar2() Dim cmdBar As mandBar Dim cmdMenu As mandBarPopup Dim cmdBtn As mandBarButton Dim cmdBuiltInBtn As mandBarButton Dim cmdBuiltInBtn2 As mandBarPopup Call DeleteMenuBar Set cmdBar = Application.mandBars(WorkSheet Menu Bar) With cmdBar Set cmdMenu = .Controls.Add
19、(Type:=msoControlPopup, temporary:=True) With cmdMenu .Caption = My Menu Set cmdBtn = .Controls.Add(Type:=msoControlButton) With cmdBtn .Caption = Item1 .OnAction = Item1Action .FaceId = 12 End With Set cmdBuiltInBtn = .Controls.Add(Type:=msoControlButton, ID:=18) Set cmdBuiltInBtn2 = .Controls.Add(10, 30017) End With End WithEnd Sub10、恢复工具栏初始状态参考:Reset mandBar.xls11、列出mandBars & mandControls ID & FaceID参考:ListAllFaceID1.xls、ListAllFaceID2.xls、ListAllFaceID3.xls、ListAllFaceID-Crdotlin.xls12、工具栏练习讲解参考:ProtectSheet.xla. . word.zl-