Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel VB.net Add-in Right click menu issue

Posted on 2013-01-12
2
Medium Priority
?
602 Views
Last Modified: 2013-01-14
Hi
I have several add-ins running in Excel that I have built. In one of them I use the code
below to get a right click menu. It is used with a specific spreadsheet that has a pivot table
and overrides the usual right click menu.
My problem is that it does this for all spreadsheets. How do I add extra control to let
Excel run its usual right click in other spreadsheets and only run the customized one when
I need it to?


Imports Office = Microsoft.Office.Core
Imports Excel = Microsoft.Office.Interop.Excel


Public Class ThisAddIn

    Dim cellbar As Office.CommandBar
    WithEvents button As Office.CommandBarButton


    Private Sub ThisAddIn_Startup() Handles Me.Startup

        Try
            'NOTE - this menu is a brnd new one and overrides the existing Excel right click
            cellbar = Me.Application.CommandBars.Add("Testing", Office.MsoBarPosition.msoBarPopup, False, True)
            If button Is Nothing Then
                ' add the button
                button = DirectCast(cellbar.Controls.Add(Office.MsoControlType.msoControlButton, , , , True), Office.CommandBarButton)
                button.Caption = "Add/Edit Tasks"
                button.BeginGroup = True
                button.Tag = "MYRIGHTCLICKMENU"
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Try

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
    Private Sub button_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles button.Click
        Call oAddEdit_Tasks()
    End Sub

    Private Sub Application_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Microsoft.Office.Interop.Excel.Range, ByRef Cancel As Boolean) Handles Application.SheetBeforeRightClick
        Cancel = True
        cellbar.ShowPopup()
    End Sub
0
Comment
Question by:Murray Brown
2 Comments
 
LVL 23

Accepted Solution

by:
Michael Fowler earned 2000 total points
ID: 38772999
You could try testing for the workbook/worksheet name in the SheetBeforeRightClick event to determine wether to cancel the default action or not
Michael
0
 

Author Closing Comment

by:Murray Brown
ID: 38773627
Thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question