Solved

Excel VB.net Add-in Right click menu issue

Posted on 2013-01-12
2
596 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:murbro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 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:murbro
ID: 38773627
Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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 article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

733 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