E
ExcelMonkey
I have a routine which adds two additional items to my right click menu. It
works really well. Except today I went to use it while my spreadsheet was in
"Page Break Preview" (i.e. View/Page Break Preview) instead of "Normal" and
the routine falied to work. Why would this matter? It works with he
following code in Normal view.
Thisworkbook module:
**********************************************8
Private AppClass As EventClass
Private Sub Workbook_Open()
Set AppClass = New EventClass
Set AppClass.App = Excel.Application
End Sub
Private Sub App_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
Call DeleteCustomMenu
End Sub
Class Module
*************************************************
Option Explicit
Public WithEvents App As Excel.Application
Private Sub App_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As
Range, Cancel As Boolean)
Call DeleteCustomMenu 'remove possible duplicates
Call BuildCustomMenu 'build new menu
End Sub
Regular Module
***************************************************
Option Explicit
Dim MyDataObj As New DataObject 'Note Reference to Microsoft Forms 2.0
needed under Tools/References
Sub BuildCustomMenu()
Dim ctrl As CommandBarControl
Dim btn As CommandBarControl
Dim i As Integer
'add a 'popup' control to the cell commandbar (menu)
Set ctrl = Application.CommandBars("Cell").Controls.Add _
(Type:=msoControlPopup, Before:=5)
ctrl.Caption = "Menu1..."
ctrl.BeginGroup = True
'add a 'popup' control to the cell commandbar (menu)
Set ctrl = Application.CommandBars("Cell").Controls.Add _
(Type:=msoControlPopup, Before:=6)
ctrl.Caption = "Menu2..."
'add the submenus
Set btn = ctrl.Controls.Add
btn.Caption = "List Correct?" 'give them a name
btn.OnAction = "ValidationCheck" 'the routine called by the control
End Sub
Sub DeleteCustomMenu()
Dim ctrl As CommandBarControl
'This line of code wil reset menu if you run into trouble.
'Comment-out if code is working
'If not working Comment in and then comment out
'the items in the For Each stmt below
'Then comment this back out and comment in
'items in for Each loop.
'Application.CommandBars("Cell").Reset
'go thru all the cell commandbar controls and delete our menu item
For Each ctrl In Application.CommandBars("Cell").Controls
If ctrl.Caption = "Menu1..." Then
ctrl.Delete
ElseIf ctrl.Caption = "Menu2..." Then
ctrl.Delete
End If
Next
End Sub
works really well. Except today I went to use it while my spreadsheet was in
"Page Break Preview" (i.e. View/Page Break Preview) instead of "Normal" and
the routine falied to work. Why would this matter? It works with he
following code in Normal view.
Thisworkbook module:
**********************************************8
Private AppClass As EventClass
Private Sub Workbook_Open()
Set AppClass = New EventClass
Set AppClass.App = Excel.Application
End Sub
Private Sub App_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
Call DeleteCustomMenu
End Sub
Class Module
*************************************************
Option Explicit
Public WithEvents App As Excel.Application
Private Sub App_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As
Range, Cancel As Boolean)
Call DeleteCustomMenu 'remove possible duplicates
Call BuildCustomMenu 'build new menu
End Sub
Regular Module
***************************************************
Option Explicit
Dim MyDataObj As New DataObject 'Note Reference to Microsoft Forms 2.0
needed under Tools/References
Sub BuildCustomMenu()
Dim ctrl As CommandBarControl
Dim btn As CommandBarControl
Dim i As Integer
'add a 'popup' control to the cell commandbar (menu)
Set ctrl = Application.CommandBars("Cell").Controls.Add _
(Type:=msoControlPopup, Before:=5)
ctrl.Caption = "Menu1..."
ctrl.BeginGroup = True
'add a 'popup' control to the cell commandbar (menu)
Set ctrl = Application.CommandBars("Cell").Controls.Add _
(Type:=msoControlPopup, Before:=6)
ctrl.Caption = "Menu2..."
'add the submenus
Set btn = ctrl.Controls.Add
btn.Caption = "List Correct?" 'give them a name
btn.OnAction = "ValidationCheck" 'the routine called by the control
End Sub
Sub DeleteCustomMenu()
Dim ctrl As CommandBarControl
'This line of code wil reset menu if you run into trouble.
'Comment-out if code is working
'If not working Comment in and then comment out
'the items in the For Each stmt below
'Then comment this back out and comment in
'items in for Each loop.
'Application.CommandBars("Cell").Reset
'go thru all the cell commandbar controls and delete our menu item
For Each ctrl In Application.CommandBars("Cell").Controls
If ctrl.Caption = "Menu1..." Then
ctrl.Delete
ElseIf ctrl.Caption = "Menu2..." Then
ctrl.Delete
End If
Next
End Sub