操作指南:在執行階段更新功能區上的控制項

本逐步解說示範如何在功能區載入到 Office 應用程式後,使用功能區物件模型來更新功能表上的控制項。

適用於: 本主題中的資訊適用於下列應用程式的文件層級專案和 VSTO 增益集專案:Excel; InfoPath 2013 和 InfoPath 2010; Outlook; PowerPoint; Project; Visio; Word。 如需詳細資訊,請參閱 Office 應用程式和專案類型所提供的功能

此範例從 Northwind 範例資料庫提取資料,以在 Microsoft Office Outlook 中填充下拉式方塊與選單。 您在這些控制項中選取的項目會自動填入電子郵件訊息中的 [收件者] 和 [主旨] 等欄位。

本逐步解說將說明下列工作:

  • 建立新的 Outlook VSTO 增益集專案。

  • 設計自定義功能區群組。

  • 將自訂群組新增至內建索引標籤。

  • 在程式執行時更新功能區上的控制項。

備註

在下列指示的某些 Visual Studio 使用者介面項目中,您的電腦可能會顯示不同的名稱或位置: 您所擁有的 Visual Studio 版本以及使用的設定會決定這些項目。 如需詳細資訊,請參閱將 Visual Studio IDE 個人化

先決條件

您需要下列組件才能完成本步驟解說:

建立新的 Outlook VSTO 增益集專案

首先,建立 Outlook VSTO 增益集專案。

建立新的 Outlook VSTO 增益集專案

  1. 在 Visual Studio 中,建立名稱為 Ribbon_Update_At_Runtime 的 Outlook VSTO 增益集專案。

  2. [新增專案 ] 對話方塊中,選取 [ 建立解決方案的目錄]。

  3. 將專案儲存至預設專案目錄。

    如需詳細資訊,請參閱 如何:在 Visual Studio 中建立 Office 專案

設計自訂功能區群組

此範例的功能區會在使用者撰寫新郵件訊息時出現。 若要建立功能區的自訂群組,請先將功能區項目新增至您的專案,然後在功能區設計工具中設計群組。 此自訂群組將協助您從資料庫中提取名稱和訂單歷史記錄,以產生後續電子郵件給客戶。

要設計一個自訂群組

  1. [專案] 功能表上,按一下 [新增專案]。

  2. 在 [新增項目] 對話方塊中,選取 [功能區(視覺化設計工具)]。

  3. 將新功能區的名稱變更為 CustomerRibbon,然後按一下 [新增]。

    CustomerRibbon.csCustomerRibbon.vb檔案會在功能區設計工具中開啟,並顯示預設索引標籤和群組。

  4. 按一下功能區設計器以選取它。

  5. [屬性] 視窗中,按一下 RibbonType 屬性旁的下拉式箭號,然後按一下 Microsoft.Outlook.Mail.Compose

    這可讓功能區在使用者在 Outlook 中撰寫新郵件時顯示。

  6. 在功能區設計工具中,按一下 [群組 1 ] 以選取它。

  7. [屬性] 視窗中,將 [標籤] 設定為 [客戶購買]。

  8. [工具箱] 的 [Office 功能區控制項] 索引標籤中,將 ComboBox 拖曳至 [客戶購買] 群組。

  9. 按一下 ComboBox1 以選取它。

  10. [屬性] 視窗中,將 [標籤] 設定為 [客戶]。

  11. [工具箱] 的 [Office 功能區控制項] 索引標籤中,將 [功能表] 拖曳到 [客戶購買] 群組。

  12. 屬性 視窗中,將 標籤 設定為 產品已購買

  13. Dynamic 設定為 true

    這可讓您在功能區載入到 Office 應用程式之後,在執行階段新增和移除功能表上的控制項。

將自訂群組新增至內建索引標籤

內建索引標籤是 Outlook 檔案總管或偵測器功能區上已有的索引標籤。 在此程序中,您會將自訂群組新增至內建索引標籤,然後指定自訂群組在索引標籤上的位置。

若要將自訂群組新增至預設索引標籤

  1. 按一下 TabAddins (Built-In) 標籤以選取它。

  2. [屬性] 視窗中,展開 ControlId 屬性,然後將 OfficeId 設定為 TabNewMailMessage

    這會將 客戶購買 群組新增至新郵件訊息中顯示的功能區選項卡的 訊息 索引標籤。

  3. 按一下「 客戶購買 」群組以選取它。

  4. [屬性] 視窗中,展開 [Position ] 屬性,按一下 PositionType 屬性旁的下拉式箭號,然後按一下 [BeforeOfficeId]。

  5. OfficeId 屬性設定為 GroupClipboard

    這會將「客戶購買」群組放置在「訊息」標籤的「剪貼簿」群組之前。

建立資料來源

使用「 資料來源」 視窗將類型化資料集新增至您的專案。

建立資料來源

  1. [資料 ] 功能表上,按一下 [新增資料來源]。

    這會啟動 資料來源設定精靈

  2. 選取 資料庫,然後按一下 下一步

  3. 選取 資料集,然後按一下 下一步

  4. 選取 Northwind 範例 Microsoft SQL Server Compact 4.0 資料庫的資料連線,或使用 [新增連線] 按鈕新增連線。

  5. 選取或建立連線之後,按一下 下一步

  6. 按一下 [下一步 ] 以儲存連接字串。

  7. 在 [選擇您的資料庫物件] 頁面上,展開 [資料表]。

  8. 選取下列每個表格旁邊的核取方塊:

    1. 客戶

    2. 訂單詳情

    3. 產品

  9. 按一下完成

在執行階段更新自訂群組中的控制項

使用功能區物件模型來執行下列工作:

  • 將客戶名稱新增至 [客戶] 下拉式方塊。

  • 將功能表和按鈕控制項新增至 [ 購買的產品] 功能表,以代表銷售訂單和已售產品。

  • 使用「 客戶 」下拉式方塊及「 購買的產品 」功能表中的資料,填入新郵件訊息的「收件者」、「主旨」及「內文」欄位。

使用 Ribbon 物件模型更新自訂群組中的控制項

  1. 「專案 」功能表上,按一下 「新增參照」。

  2. 在 [ 新增參考 ] 對話方塊中,按一下 [.NET ] 索引標籤,選取 System.Data.Linq 元件,然後按一下 [ 確定]。

    此元件包含使用 Language-Integrated Query (LINQ) 的類別。 您將使用 LINQ 以 Northwind 資料庫中的資料填入自訂群組中的控制項。

  3. [方案總管] 中,按一下 CustomerRibbon.csCustomerRibbon.vb 以選取它。

  4. [檢視 ] 功能表上,按一下 [程式碼]。

    功能區程式碼檔案會在程式碼編輯器中開啟。

  5. 將下列陳述式新增至功能區程式碼檔案的頂端。 這些陳述式可讓您輕鬆存取 LINQ 命名空間,以及 Outlook 主要互通元件 (PIA) 的命名空間。

    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    using System.Linq.Expressions;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using System.Data;
    using System.IO;
    using Ribbon_Update_At_Runtime.Northwind40DataSetTableAdapters;
    
  6. 在類別內 CustomerRibbon 新增下列程式碼。 此程式碼會宣告資料表和資料表配接器,您將會用來儲存 Northwind 資料庫的 Customer、Orders、訂單詳細資料和 Product 資料表中的資訊。

    //Declare the Northwind dataset.
    Northwind40DataSet nwDataSet = new Northwind40DataSet();
    
    //Declare the data tables.
    
    Northwind40DataSet.CustomersDataTable customerTable;
    Northwind40DataSet.OrdersDataTable orderTable;
    Northwind40DataSet.Order_DetailsDataTable orderDetailsTable;
    Northwind40DataSet.ProductsDataTable productsTable;
    
    //Declare the data table adapters for each table.
    
    CustomersTableAdapter customerTableAdapter = new CustomersTableAdapter();
    OrdersTableAdapter ordersTableAdapter = new OrdersTableAdapter();
    Order_DetailsTableAdapter detailsTableAdapter = new Order_DetailsTableAdapter();
    ProductsTableAdapter productsTableAdapter = new ProductsTableAdapter();
    
  7. 將下列程式碼 CustomerRibbon 區塊新增至類別。 此程式碼會新增三個輔助方法,以在執行階段建立功能區上的控制項。

    private RibbonDropDownItem CreateRibbonDropDownItem()
    {
        return this.Factory.CreateRibbonDropDownItem();
    }
    private RibbonMenu CreateRibbonMenu()
    {
        return this.Factory.CreateRibbonMenu();
    }
    private RibbonButton CreateRibbonButton()
    {
        RibbonButton button = this.Factory.CreateRibbonButton();
        button.Click += new RibbonControlEventHandler(button_Click);
        return button;
    }
    
  8. CustomerRibbon_Load 事件處理器方法替換為下列程式碼。 此程式碼會使用 LINQ 查詢來執行下列工作:

    • 使用 Northwind 資料庫中 20 個客戶的識別碼和名稱,填充到 [客戶] 下拉式方塊中。

    • 呼叫 PopulateSalesOrderInfo 輔助方法。 此方法會使用與目前所選客戶相關的銷售訂單編號來更新 ProductsPurchased 功能表。

      private void CustomerRibbon_Load(object sender, RibbonUIEventArgs e)
      {
          customerTable = nwDataSet.Customers;
          customerTableAdapter.Fill(customerTable);
      
          var customerQuery = from customers in customerTable.AsEnumerable().Take(20)
                              select new
                              {
                                  CustomerID = customers.Field<string>("Customer ID"),
                                  CustomerName = customers.Field<string>("Contact Name")
                              };
      
      
          // Execute the query.
          foreach (var item in customerQuery)
          {
              this.comboBox1.Items.Add(CreateRibbonDropDownItem());
              this.comboBox1.Items.Last().Label =
              item.CustomerName + "|" + item.CustomerID.ToString();
          }
          this.comboBox1.Text = this.comboBox1.Items.First().Label;
          PopulateSalesOrderInfo();
      }
      

  9. 將下列程式碼 CustomerRibbon 新增至類別。 此程式碼會使用 LINQ 查詢來執行下列工作:

    • 將一個子功能表新增到 ProductsPurchased 功能表中,以對應每一個與所選客戶相關的銷售訂單。

    • 將按鈕新增至與銷售訂單相關產品的每個子菜單。

    • 將事件處理常式新增至每個按鈕。

      private void PopulateSalesOrderInfo()
      {
          String[] tempArray = comboBox1.Text.Split(new Char[] { '|' });
          menu1.Items.Clear();
      
          orderTable = nwDataSet.Orders;
          orderDetailsTable = nwDataSet.Order_Details;
          productsTable = nwDataSet.Products;
      
          ordersTableAdapter.Fill(orderTable);
          detailsTableAdapter.Fill(orderDetailsTable);
          productsTableAdapter.Fill(productsTable);
      
          var orderQuery = from orders in orderTable.AsEnumerable()
                           where orders.Field<string>("Customer ID") == tempArray[1]
                           select new { OrderID = orders.Field<int>("Order ID") };
      
          foreach (var orderItem in orderQuery)
          {
              menu1.Items.Add(CreateRibbonMenu());
      
              RibbonMenu orderMenu = (RibbonMenu)menu1.Items.Last();
              orderMenu.Dynamic = true;
              orderMenu.Label = orderItem.OrderID.ToString();
              orderMenu.Tag = orderItem.OrderID;
      
              var productQuery = from orderDetail in orderDetailsTable.AsEnumerable()
                                 join product in productsTable.AsEnumerable() on
                                     orderDetail.Field<int>("Product ID")
                                 equals product.Field<int>("Product ID")
                                 where orderDetail.Field<int>("Order ID") ==
                                     orderItem.OrderID
                                 select new { ProductName = product.Field<string>("Product Name") };
      
              foreach (var productItem in productQuery)
              {
                  RibbonButton button = CreateRibbonButton();
                  button.Label = productItem.ProductName;
                  orderMenu.Items.Add(button);
              }
          }
      }
      

  10. [方案總管] 中,按兩下功能區程式碼檔案。

    功能區設計工具隨即開啟。

  11. 在功能區設計工具中,按兩下 [客戶] 下拉式方塊。

    功能區的程式碼檔案會在程式碼編輯器中開啟,並顯示 ComboBox1_TextChanged 事件處理常式。

  12. 請將 ComboBox1_TextChanged 事件處理程序替換為以下程式碼。 此程式碼會執行下列工作:

    • 呼叫 PopulateSalesOrderInfo 輔助方法。 此方法會使用與所選客戶相關的銷售訂單來更新「 已購買的產品」 功能表。

    • 呼叫 PopulateMailItem 協助程式方法,並傳入目前的文字,也就是選取的客戶名稱。 這個方法會填入新郵件訊息的「收件者」、「主旨」和「內文」欄位。

      private void comboBox1_TextChanged(object sender,
          RibbonControlEventArgs e)
      {
          PopulateSalesOrderInfo();
          PopulateMailItem(comboBox1.Text);
      }
      

  13. 將下列 Click 事件處理常式新增至 CustomerRibbon 類別。 此程式碼會將所選產品的名稱新增至新郵件訊息的 內文 欄位。

    void button_Click(object sender, RibbonControlEventArgs e)
    {
        Outlook.Application application = Globals.ThisAddIn.Application;
        Outlook.Inspector inspector = application.ActiveInspector();
        Outlook.MailItem myMailItem = (Outlook.MailItem)inspector.CurrentItem;
        RibbonButton myCheckBox = (RibbonButton)sender;
        myMailItem.Subject = "Following up on your order";
        myMailItem.Body = myMailItem.Body + "\n" + "* " + myCheckBox.Label;
    }
    
  14. 將下列程式碼 CustomerRibbon 新增至類別。 此程式碼會執行下列工作:

    • 使用目前所選客戶的電子郵件地址填入新郵件訊息的 [收件者] 行。

    • 將文字新增至新郵件訊息的「主旨」和「內文」欄位。

      private void PopulateMailItem(string addressToLine)
      {
          Outlook.Application application = Globals.ThisAddIn.Application;
          Outlook.Inspector inspector = application.ActiveInspector();
          Outlook.MailItem myMailItem = (Outlook.MailItem)inspector.CurrentItem;
      
          myMailItem.To = "";
          String[] tempArray = addressToLine.Split(new Char[] { '|' });
          myMailItem.To = tempArray[0] + "@example.com";
          myMailItem.Subject = "Following up on your order";
          myMailItem.Body = "Hello " + tempArray[0] + "," +
              "\n" + "We would like to get your feedback on the " +
              "following products that you recently ordered: ";
      }
      

測試自訂群組中的控制項

當您在 Outlook 中開啟新的郵件表單時,功能區的 [訊息] 索引標籤上會出現名為 [客戶購買] 的自訂群組。

若要建立客戶後續電子郵件訊息,請選取客戶,然後選取客戶購買的產品。 [ 客戶購買 ] 群組中的控制項會在執行階段使用 Northwind 資料庫中的資料進行更新。

測試自訂群組中的控制項

  1. 請按 F5 執行您的專案。

    Outlook 開始。

  2. 在 Outlook 的 [ 檔案 ] 功能表上,指向 [ 新增],然後按一下 [郵件訊息]。

    會出現下列動作:

    • 出現新的郵件訊息「檢查器」視窗。

    • 在功能區的 [訊息] 索引標籤上,[ 客戶購買] 群組會出現在 [剪貼簿] 群組之前。

    • 群組中的 客戶組合方塊會更新為 Northwind 資料庫中的客戶名稱。

  3. 在功能區的[訊息]索引標籤中的[客戶購買]群組中,從[客戶]下拉式方塊中選取客戶。

    會出現下列動作:

    • 購買的產品」 功能表會更新,以顯示所選客戶的每個銷售訂單。

    • 每個銷售訂單的子功能表都會更新,以顯示該訂單中購買的產品。

    • 所選客戶的電子郵件地址會新增至郵件訊息的「 收件者 」行,且郵件訊息的主旨和內文會填入文字。

  4. 按一下 [產品購買 ] 功能表,指向任何銷售訂單,然後按一下銷售訂單中的產品。

    產品名稱會新增至郵件訊息的內文。

後續步驟

您可以從下列主題深入瞭解如何自訂 Office UI: