2013-06-25 22 views
7

E 'possibile disabilitare o configurare il menu di scelta rapida (cella, intervallo) tramite C# (VSTO). Se sì, come posso implementarlo (in un documento a livello di applicazione VSTO Excel)VSTO (a livello di documento): menu contestuale individuale in Excel (menu clic destro)

Ad esempio voglio disabilitare alcune voci del menu di contesto (ad esempio copia/incolla) e aggiungere nuovi elementi o sostituire il menu di scelta rapida standard con un menu completo!

Are Smarttags una buona alternativa ai menu contestuali in Excel?

+1

Sì, è possibile con Globals.ThisWorkbook.Application.Commandbars. Non l'ho fatto da un po ', ma questo dovrebbe farti iniziare. – rwisch45

risposta

11
  • Ecco alcuni esempi di codice di massima con i commenti
  • Questo è stato creato nel VS2010 e testato contro Excel 2010
  • primo passo è stato quello creato una nuova Excel 2010 Add-In progetto
  • Quindi aggiunto il seguente codice di esempio al codice predefinito generato all'interno di ThisAddin.cs.
  • Questo codice aggiungerà una nuova voce di menu e rimuoverà le voci di menu taglia/copia/incolla quando si fa clic destro su una singola cella contenente 'abc'. Questo per simulare la modifica del menu di scelta rapida in base al contenuto di una cella.
 


    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Xml.Linq; 
    using Excel = Microsoft.Office.Interop.Excel; 
    using Office = Microsoft.Office.Core; 
    using Microsoft.Office.Tools.Excel; 
    using System.Diagnostics; 
    using Microsoft.Office.Interop.Excel; 

    namespace Excel_Menu 
    { 
     public partial class ThisAddIn 
     { 
      private void ThisAddIn_Startup(object sender, System.EventArgs e) 
      { 
       ResetCellMenu(); // reset the cell context menu back to the default 

       // Call this function is the user right clicks on a cell 
       this.Application.SheetBeforeRightClick+=new Excel.AppEvents_SheetBeforeRightClickEventHandler(Application_SheetBeforeRightClick); 
      } 

      private void ThisAddIn_Shutdown(object sender, System.EventArgs e) 
      { 
      } 

      private Office.CommandBar GetCellContextMenu() 
      { 
       return this.Application.CommandBars["Cell"]; 
      } 

      private void ResetCellMenu() 
      { 
       GetCellContextMenu().Reset(); // reset the cell context menu back to the default 
      } 

      private void Application_SheetBeforeRightClick(object Sh, Range Target, ref bool Cancel) 
      { 
       ResetCellMenu(); // reset the cell context menu back to the default 

       if (Target.Cells.Count == 1) // sample code: if only a single cell is selected 
       { 
        if (Target.Cells[1, 1].Value == "abc") // sample code: if the signle cell contains 'abc' 
        { 
         AddExampleMenuItem(); 
         RemoveCutCopyPasteMenuItems(); 
        } 
       } 
      } 

      private void AddExampleMenuItem() 
      { 
       Office.MsoControlType menuItem = Office.MsoControlType.msoControlButton; 
       Office.CommandBarButton exampleMenuItem = (Office.CommandBarButton)GetCellContextMenu().Controls.Add(menuItem, missing, missing, 1, true); 

       exampleMenuItem.Style = Office.MsoButtonStyle.msoButtonCaption; 
       exampleMenuItem.Caption = "Example Menu Item"; 
       exampleMenuItem.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(exampleMenuItemClick); 
      } 

      private void RemoveCutCopyPasteMenuItems() 
      { 
       Office.CommandBar contextMenu = GetCellContextMenu(); 

       for (int i = contextMenu.Controls.Count; i > 0; i--) 
       { 
        Office.CommandBarControl control = contextMenu.Controls[i]; 

        if (control.Caption == "Cu&t") control.Delete(); // Sample code: remove cut menu item 
        else if (control.Caption == "&Copy") control.Delete(); // Sample code: remove copy menu item 
        else if (control.accDescription.Contains("Paste")) control.Delete(); // Sample code: remove any paste menu items 
       } 
      } 

      void exampleMenuItemClick(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault) 
      { 
       System.Windows.Forms.MessageBox.Show("Example Menu Item clicked"); 
      } 

      #region VSTO generated code 

      /// 
      /// Required method for Designer support - do not modify 
      /// the contents of this method with the code editor. 
      /// 
      private void InternalStartup() 
      { 
       this.Startup += new System.EventHandler(ThisAddIn_Startup); 
       this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); 
      } 

      #endregion 
     } 
    } 

 
+0

Grazie. Ma questa è la soluzione per un progetto VSTO Add-In/Application-Level. Ho bisogno di una soluzione per un'applicazione Document-Level !! –

+0

Non ho ancora creato un componente aggiuntivo a livello di documento, ma ho appena creato una nuova cartella di lavoro di Excel 2010. Corrisponde a come lo stai usando? Sono stato in grado di utilizzare il codice di cui sopra in ThisWorkbook.cs e ha funzionato allo stesso modo. –

Problemi correlati