2013-06-25 97 views
7

Có thể tắt hoặc định cấu hình trình đơn ngữ cảnh (ô, dải) qua C# (VSTO). Nếu có, làm thế nào tôi có thể thực hiện nó (trong một tài liệu cấp ứng dụng VSTO Excel)VSTO (Cấp tài liệu): Trình đơn ngữ cảnh riêng lẻ trong Excel (menu chuột phải)

Ví dụ tôi muốn vô hiệu hóa một số mặt hàng trong menu ngữ cảnh (ví dụ như copy/paste) và thêm các mục mới hoặc thay thế menu ngữ cảnh chuẩn với menu hoàn chỉnh riêng!

Có phải Smarttags là lựa chọn tốt cho menu ngữ cảnh trong Excel không?

+1

Có thể với Globals.ThisWorkbook.Application.Commandbars. Tôi đã không thực hiện nó trong một thời gian, nhưng điều đó sẽ giúp bạn bắt đầu. – rwisch45

Trả lời

11
  • Dưới đây là một số mẫu mã thô với ý kiến ​​
  • này được tạo ra trong VS2010 và thử nghiệm chống lại Excel 2010
  • Bước đầu tiên là để tạo ra một mới Excel 2010 Add-In dự án
  • Sau đó, thêm mã mẫu bên dưới vào mã mặc định được tạo bên trong ThisAddin.cs.
  • Mã này sẽ thêm một mục trình đơn mới và xóa các mục menu cắt/sao chép/dán khi nhấp chuột phải vào một ô có chứa 'abc'. Điều này là để mô phỏng việc thay đổi menu ngữ cảnh dựa trên nội dung của một ô.
 


    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

Cảm ơn bạn. Nhưng đây là giải pháp cho dự án VSTO Add-In/Application-Level. Tôi cần một giải pháp cho một ứng dụng Document-Level !! –

+0

Tôi chưa tạo bổ trợ cấp tài liệu trước đây nhưng tôi vừa tạo một ứng dụng Excel 2010 Workbook mới. Điều đó phù hợp với cách bạn đang sử dụng nó? Tôi đã có thể sử dụng mã từ bên trên trong ThisWorkbook.cs và nó hoạt động tương tự. –