|
|
|
|
|
|
Excel
|
Create menu for use in international environment distributed as xla
|
|
Ease of Use
|
Intermediate
|
Version tested with
|
2003
|
Submitted by:
|
tpoynton
|
Description:
|
The code provided here will allow you to create custom menu items that work in an international environment.
|
Discussion:
|
A popular way to create custom menu items in VBA is documented in this KB entry (http://vbaexpress.com/kb/getarticle.php?kb_id=427). However, in international environments, using "Worksheet Menu Bar" and "Help" (for example) as references does not always work, because "Worksheet Menu Bar" and/or "Help" may be called something else in other language versions of Excel. A simple alternative is to use the control ID #'s instead of the string names. A list of the control ID's can be found on Microsoft's website at http://support.microsoft.com/default.aspx?scid=kb;%5BLN%5D;Q213552 . The code below is geared towards developing a menu-driven Excel addin that is distributed as an xla file. Where code needs to be inserted in the VBE and the steps needed to create a redisributable xla are provided in comments embedded in the code below.
|
Code:
|
instructions for use
|
Option Explicit
Private Sub Workbook_AddinInstall()
Dim ctrlMain As CommandBarPopup
Dim ctrlItem As CommandBarControl
Dim iHelpIndex As Long
Dim cbHelp As CommandBarControl
Set cbHelp = Application.CommandBars(1).FindControl(ID:=30010)
iHelpIndex = cbHelp.Index
KillMenu
Set ctrlMain = Application.CommandBars(1).Controls.Add _
(Type:=msoControlPopup, Before:=iHelpIndex, temporary:=False)
With ctrlMain
.Caption = "&My New Menu"
Set ctrlItem = .Controls.Add(Type:=msoControlButton)
With ctrlItem
.Caption = "&Item #1"
.OnAction = "ThisWorkbook.doItem1"
End With
Set ctrlItem = _
.Controls.Add(Type:=msoControlButton)
With ctrlItem
.Caption = "&Item #2"
.OnAction = "ThisWorkbook.doItem2"
End With
End With
End Sub
Private Sub Workbook_AddinUninstall()
KillMenu
Dim xlaName As String
Dim i As Integer
With ThisWorkbook
For i = 1 To AddIns.Count
xlaName = AddIns(i).Name
If xlaName = "sampleMenu.xla" Then
AddIns(i).Installed = False
End If
Next i
End With
End Sub
Sub KillMenu()
Dim cmdBar As CommandBar
Set cmdBar = Application.CommandBars(1)
On Error Resume Next
Set cmdBar = Application.CommandBars(1)
cmdBar.Controls("&My New Menu").Delete
On Error GoTo 0
End Sub
Sub doItem1()
MsgBox ("Thanks for clicking on Item #1!")
End Sub
Sub doItem2()
MsgBox ("Thanks for clicking on Item #2!")
End Sub
|
How to use:
|
- open the VBE by pressing alt+F11
- insert the code into ThisWorkbook
- modify code as desired
- save file as type xla in Excel, not VBE
- go to Tools>Add Ins in Excel
- click Browse (or Select on Mac) to navigate to location where you saved the xla
- highlight xla file name and click OK twice
- your new menu should appear
|
Test the code:
|
- save the sample xla file to your computer
- go to Tools>Add Ins in Excel
- click Browse (or Select on Mac) to navigate to location where you saved sampleMenu.xla
- highlight sampleMenu.xla file and click OK twice
- New menu named "My New Menu" should appear next to Help menu
|
Sample File:
|
sampleMenu.zip 10.55KB
|
Approved by mdmackillop
|
This entry has been viewed 286 times.
|
|