2013-06-25 2 views
7

क्या सी # (वीएसटीओ) के माध्यम से (सेल, रेंज) संदर्भ मेनू को अक्षम या कॉन्फ़िगर करना संभव है। यदि हाँ, मैं इसे कैसे लागू कर सकते हैं (एक दस्तावेज़ स्तरीय VSTO एक्सेल आवेदन में)वीएसटीओ (दस्तावेज़-स्तर): एक्सेल में व्यक्तिगत संदर्भ मेनू (राइट क्लिक मेनू)

उदाहरण के लिए मैं (जैसे कॉपी/पेस्ट) संदर्भ मेनू में कुछ आइटम को अक्षम करें और नए आइटम जोड़ने या बदलना चाहते हैं एक पूर्ण खुद के मेनू के साथ मानक संदर्भ मेनू!

स्मार्टटैग एक्सेल में संदर्भ मेनू के लिए एक अच्छा विकल्प है?

+1

हाँ, यह Globals.ThisWorkbook.Application.Commandbars साथ संभव है। मैंने इसे थोड़ी देर में नहीं किया है, लेकिन आपको इसे शुरू करना चाहिए। – rwisch45

उत्तर

11
  • यहाँ
  • यह VS2010 में बनाया गया था और के खिलाफ परीक्षण Excel 2010 टिप्पणी के साथ कुछ किसी न किसी नमूना कोड
  • प्रथम चरण के लिए गया था एक नया Excel 2010 बनाया ऐड-में परियोजना
  • फिर नीचे के अंदर उत्पन्न डिफ़ॉल्ट कोड में नमूना कोड जोड़ा गया यह Addin.cs
  • यह कोड एक नया मेनू आइटम जोड़ देगा और 'एबीसी' युक्त एक सेल पर राइट-क्लिक करते समय कट/कॉपी/पेस्ट मेनू आइटम हटा देगा। यह सेल की सामग्री के आधार पर संदर्भ मेनू को बदलने का अनुकरण करना है।
 


    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

धन्यवाद। लेकिन यह ऐड-इन/एप्लिकेशन-लेवल वीएसटीओ प्रोजेक्ट का समाधान है। मुझे दस्तावेज़-स्तर के आवेदन के लिए समाधान की आवश्यकता है !! –

+0

मैंने पहले दस्तावेज़-स्तर एड-इन नहीं बनाया है, लेकिन मैंने अभी एक नया एक्सेल 2010 वर्कबुक एप्लिकेशन बनाया है। क्या इससे मेल खाता है कि आप इसका उपयोग कैसे कर रहे हैं? मैं उपरोक्त से कोड को इस वर्कबुक.cs में उपयोग करने में सक्षम था और यह वही काम करता था। –

संबंधित मुद्दे