Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Techguy Sr eng.   on Mar 13 In MS Office Category.

  
Question Answered By: Volney Fischer   on Mar 13

You can certainly make changes to a commandbar or create a new commandbar. I
thought the question was how to add menus  to a userform. Reading the origianl
post again, I don't know where I got that idea. Here is code  to add an option to
the bottom of the Tools menu  while that workbook is open. Paste this code into a
VBA module in the workbook.

Option Explicit
Global Const VbaMenuItem As String = "MyMacroMenu"

Private Sub Auto_Open()
Dim newItem As CommandBarControl
On Error GoTo AOerr1
'Add a new item to the bottom of the Tools menu while the document is open.
If
Right(CommandBars("Tools").Controls(CommandBars("Tools").Controls.Count).Caption\
, _
Len(VbaMenuItem$)) <> VbaMenuItem$ Then
Set newItem = CommandBars("Tools").Controls.Add(Type:=msoControlButton)
With newItem
.BeginGroup = True
.Caption = VbaMenuItem$
.FaceId = 0
.OnAction = "ShowForm"
End With
End If
'Assign macro shortcut = {Ctrl}{Shift}a
Application.OnKey "+^a", "ShowForm"
Exit Sub
AOerr1:
MsgBox Err.Description
Resume Next
End Sub

Private Sub Auto_Close()
Dim x As Integer
On Error GoTo AcERR1
'Check the name of the active workbook. If it's not ThisWorkbook, don't do
anything.
If LCase(ActiveWorkbook.Name) = LCase(ThisWorkbook.Name) Then
'Remove VbaMenuItem$ from the Tools menu
For x% = 1 To CommandBars("Tools").Controls.Count
If CommandBars("Tools").Controls(x%).Caption = VbaMenuItem$ Then
CommandBars("Tools").Controls(x%).Delete
End If
Next x%
End If
'Clear macro shortcut = {Ctrl}{Shift}a
Application.OnKey "+^a", ""
Exit Sub
AcERR1:
MsgBox Err.Description
Resume Next
End Sub

You could use the Workbook_Open and Workbook_BeforeClose events instead of
Auto_Open and Auto_Close. In that case, the code would go in the ThisWorkbook
module.

Share: 

 

This Question has 5 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Menus in VBA Excel Or get search suggestion and latest updates.


Tagged: