星期三, 二月 07, 2007

Tips VBA office 热键的编写方法

Office 的帮助文件一般放在office安装目录下。比如:\OFFICE11\2052\VBAOF11.CHM

VBA里面经常需要把一个热键(VBA里称为shortcutKey)赋予一个宏或一个菜单。

 热键包括组合键盘比如ctrl或alt加上另外一个键来使用。
Ctrl在VBA里表示用^
Alt在VBA里表示用&

http://www.romanpress.com/Articles/Menus_R/Menus.htm

比如把一个热键赋予一个菜单项目.

Dim cbMainMenuBar As CommandBar
Dim iHelpMenu As Integer
Dim cbcCutomMenu As CommandBarControl

Set cbMainMenuBar = _
Application.CommandBars("Worksheet Menu Bar")

Set cbcCutomMenu = _
cbMainMenuBar.Controls.Add(Type:=msoControlPopup)

cbcCutomMenu.Caption = "&Q功能菜单" 'alt+q

星期六, 二月 03, 2007

Tips EXCEL VBA里如何增加菜单及菜单项目风格线

在vbproject ThisWorkbook 里输入以下代码

Sub AddMenus()
Dim cMenu1 As CommandBarControl
Dim cbMainMenuBar As CommandBar
Dim iHelpMenu As Integer
Dim cbcCutomMenu As CommandBarControl

Set cbMainMenuBar = _
Application.CommandBars("Worksheet Menu Bar")

Set cbcCutomMenu = _
cbMainMenuBar.Controls.Add(Type:=msoControlPopup)

cbcCutomMenu.Caption = "&Q功能菜单"

With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "简约方式"
.OnAction = "forcaseGS3"
End With

With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "简约方式MOD"
.OnAction = "forcaseGSMod3"

End With

With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "打印机设置"
.OnAction = "printerSetup"
.BeginGroup = True '在这个菜单前产生一个分割线
End With


End Sub


Sub DeleteMenu()
On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("&Q功能菜单").Delete
On Error GoTo 0
End Sub


Private Sub Workbook_Activate()
AddMenus
End Sub


Private Sub Workbook_Deactivate()
DeleteMenu
End Sub