moonshadow91
New Member
- Joined
- Jun 12, 2014
- Messages
- 7
Hi,
I have developed some macros which help daily tasks in our office. They are saved in .xlam file. When the file is open, it creates right click menu. Everything worked fine with office 2010. Now we upgraded to office 2013 and office 2016. All macros work fine, but I have a problem with the right click menu - it appears multiple times (see screenshot below).
Here is the code for the custom menu:
I also noticed it does not happen with all files – for some there is only 1 menu, for other 3,4 and etc…
Thank you very much in advance!
I have developed some macros which help daily tasks in our office. They are saved in .xlam file. When the file is open, it creates right click menu. Everything worked fine with office 2010. Now we upgraded to office 2013 and office 2016. All macros work fine, but I have a problem with the right click menu - it appears multiple times (see screenshot below).
Here is the code for the custom menu:
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
DeleteTheMenu
End Sub
Private Sub Workbook_Open()
On Error Resume Next
MakeTheMenu
End Sub
Code:
Option Explicit
Sub MakeTheMenu()
On Error Resume Next
Application.ScreenUpdating = False
DeleteTheMenu
Dim myBar As Object, myMenuItem As CommandBarControl
Dim RowNo As Long, MenuRowNo As Long
Dim CmdBar As CommandBar
RowNo = 4
Select Case ThisWorkbook.Sheets("Menu").Range("B2").Value
Case "Right-Click Menu"
For Each CmdBar In Application.CommandBars
If CmdBar.Name = "Cell" Then
Debug.Print CmdBar.Index
Set myBar = Application.CommandBars(CmdBar.Index).Controls.Add(Type:=msoControlPopup)
With myBar
.Caption = ThisWorkbook.Sheets("Menu").Range("B1").Value
.Tag = ThisWorkbook.Sheets("Menu").Range("B1").Value
.BeginGroup = True
End With
RowNo = 4
MenuRowNo = 0
GoSub fill
End If
Next
Application.ScreenUpdating = True
Exit Sub
fill:
Do While ThisWorkbook.Sheets("Menu").Cells(RowNo, 1).Value <> ""
Select Case ThisWorkbook.Sheets("Menu").Cells(RowNo, 1).Value
Case 1
Set myMenuItem = myBar.Controls.Add(msoControlPopup)
With myMenuItem
.Caption = ThisWorkbook.Sheets("Menu").Cells(RowNo, 2)
End With
MenuRowNo = 0
Case 2
With myMenuItem.Controls.Add(Type:=msoControlButton)
If MenuRowNo < 10 Then
.Caption = ThisWorkbook.Sheets("Menu").Cells(RowNo, 2).Value
Else
.Caption = ThisWorkbook.Sheets("Menu").Cells(RowNo, 2).Value
End If
.OnAction = ThisWorkbook.Sheets("Menu").Cells(RowNo, 3).Value
.BeginGroup = ThisWorkbook.Sheets("Menu").Cells(RowNo, 4).Value
.FaceId = ThisWorkbook.Sheets("Menu").Cells(RowNo, 5).Value
End With
Case 3
With myBar.Controls.Add(Type:=msoControlButton)
If MenuRowNo < 10 Then
.Caption = ThisWorkbook.Sheets("Menu").Cells(RowNo, 2).Value
Else
.Caption = ThisWorkbook.Sheets("Menu").Cells(RowNo, 2).Value
End If
.OnAction = ThisWorkbook.Sheets("Menu").Cells(RowNo, 3).Value
.BeginGroup = ThisWorkbook.Sheets("Menu").Cells(RowNo, 4).Value
.FaceId = ThisWorkbook.Sheets("Menu").Cells(RowNo, 5).Value
End With
End Select
RowNo = RowNo + 1
MenuRowNo = MenuRowNo + 1
Loop
Return
End Sub
Sub DeleteTheMenu()
On Error Resume Next
CommandBars.FindControl(Tag:=ThisWorkbook.Sheets("Menu").Range("B1").Value).Delete
CommandBars(ThisWorkbook.Sheets("Menu").Range("B1").Value).Delete
End Sub
I also noticed it does not happen with all files – for some there is only 1 menu, for other 3,4 and etc…
Thank you very much in advance!