Page Break Preview - context menu

SteveGoodman

New Member
Joined
Sep 19, 2002
Messages
41
I am trying to add a menu item to the bottom of the context menu that appears when you right click on a cell in the Page Break Preview mode. I can get it to appear on the when we are in the Normal mode, but can't get one to appear in this preview mode.

All I think is the problem is that I have the wrong id for menu. I was using the id because I thought that both menus were called CELL.

Any help would be great, here is my code.

Do While True
Set cbReporting = Application.CommandBars(iMenuID)
Set cbcReporting = cbReporting.FindControl(msoControlPopup, , gsAppName)

If Not (cbcReporting Is Nothing) Then cbcReporting.DELETE

'Now the cell popup
Set cbcReporting = Application.CommandBars(iMenuID).Controls.Add(msoControlPopup)
cbcReporting.Caption = gsAppName
cbcReporting.Tag = gsAppName
cbcReporting.BeginGroup = True

Set cbReporting = cbcReporting.CommandBar
cbReporting.Controls.Add msoControlButton, , , 1, True
cbReporting.Controls(1).Caption = "menuItem"
cbReporting.Controls(1).OnAction = "MenuItem"
cbReporting.Controls(1).FaceId = 462

If iMenuID = 27 Then Exit Do
iMenuID = 27
Loop
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
The ID for the Cells shortcut menu is 25 in normal view and 28 in Page Break Preview.

As an aside this code will list them all:

Code:
Sub ListShortCutMenus()
    Dim r As Long
    Dim cBar As CommandBar
    Dim c As Integer
    r = 1
    For Each cBar In CommandBars
        If cBar.Type = msoBarTypePopup Then
            Cells(r, 1) = cBar.Index
            Cells(r, 2) = cBar.Name
            For c = 1 To cBar.Controls.Count
                Cells(r, c + 2) = cBar.Controls(c).Caption
            Next c
            r = r + 1
        End If
    Next cBar
End Sub
 
Upvote 0
Just ran your code on my Office 97 test box and me Office 2k test box and we get different results it would appear that MS added in some new menus between the two, and no doubt it will be different again in XP, so what I need to use is the name of the menu or something else that is the same.

Am I right in thinking that the two are both called Cell?

Is there anyway this can be acheived?

Steve.
 
Upvote 0
Yes they are both called Cell - a bit careless that. And I remember reading that the index numbers changed between Excel 97 and Excel 2000.

I can't find another property to use so I guess the only way is to loop around the collection to find the ones named Cell.

I suppose we should consider ourselves lucky that we can customise the Shortcut menus and live with any shortcomings.
 
Upvote 0
ok sorted it now, here is my solution

Private Sub GetCellCommandBarIndexes()
ReDim iCellMenuPos(CommandBars.Count) As Integer 'just in case MS add some more.
Dim iCount As Integer: iCount = 0

Dim cBar As CommandBar

For Each cBar In CommandBars
If cBar.Name = "Cell" Then
iCellMenuPos(iCount) = cBar.Index
iCount = iCount + 1
End If
Next cBar

ReDim Preserve iCellMenuPos(iCount - 1) 'ok so it should be 2 integers big, but just in case.

End Sub


and then just iterate around the array like this

For i = LBound(iCellMenuPos) To UBound(iCellMenuPos)
Set cbReporting = Application.CommandBars(iCellMenuPos(i))
Set cbcReporting = cbReporting.FindControl(msoControlPopup, , gsAppName)

If Not (cbcReporting Is Nothing) Then cbcReporting.DELETE

'Now the cell popup
Set cbcReporting = Application.CommandBars(iCellMenuPos(i)).Controls.Add(msoControlPopup)
cbcReporting.Caption = gsAppName
cbcReporting.Tag = gsAppName
cbcReporting.BeginGroup = True

Set cbReporting = cbcReporting.CommandBar
cbReporting.Controls.Add msoControlButton, , , 1, True
cbReporting.Controls(1).Caption = "menuOption"
cbReporting.Controls(1).OnAction = "menuoption"
cbReporting.Controls(1).FaceId = 462
Next


But when you exit the app you must remove the menus otherwise they appear next time you open the app, this might be a good or a bad thing depending on your view point.

Public Sub TidyUpMenus()
'remove the context menus
Dim cbcReporting As CommandBarControl
Dim i As Integer

Call GetCellCommandBarIndexes

For i = LBound(iCellMenuPos) To UBound(iCellMenuPos)

Set cbcReporting = Application.CommandBars(iCellMenuPos(i)).FindControl(msoControlPopup, , gsAppName)
If Not (cbcReporting Is Nothing) Then cbcReporting.DELETE

Next i
End Sub

Hope this helps some other people

Steve.
 
Upvote 0

Forum statistics

Threads
1,215,554
Messages
6,125,487
Members
449,233
Latest member
Deardevil

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top