共用方式為


逐步解說:使用功能區 XML 建立自訂的索引標籤

本逐步解說將示範,如何使用 [功能區 (XML)] 項目建立自訂的功能區索引標籤。

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

這個逐步解說將說明下列工作:

  • 將按鈕加入至 [增益集] 索引標籤。 [增益集] 索引標籤是功能區 XML 檔案中定義的預設索引標籤。

  • 使用 [增益集] 索引標籤上的按鈕,自動化 Microsoft Office Word。

注意事項注意事項

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

必要條件

您需要下列元件才能完成此逐步解說:

-

包含 Microsoft Office 開發者工具的 Visual Studio 2010 版本。 如需詳細資訊,請參閱[設定電腦以開發 Office 方案](bb398242\(v=vs.100\).md)。
  • Microsoft Office Word 2007 或 Word 2010。 

視訊的連結 如需觀看相關示範影片,請參閱如何使用功能區設計工具自訂 Excel 中的功能區?(英文)。

建立專案

首先,必須建立 Word 增益集專案。 稍後您將自訂本文件的 [增益集] 索引標籤。

若要建立新的專案

  • 建立名稱為 MyRibbonAddIn 的 [Word 增益集] 專案。

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

    Visual Studio 會開啟 ThisAddIn.cs 或 ThisAddIn.vb 程式碼檔案,並將 MyRibbonAddIn 專案加入至 [方案總管]。

建立增益集索引標籤

若要建立 [增益集] 索引標籤,請將 [功能區 (XML)] 項目加入至您的專案。 在此逐步解說稍後的內容中,您會在這個索引標籤中加入數個按鈕。

若要建立增益集索引標籤

  1. 在 [專案] 功能表上,按一下 [加入新項目]。

  2. 在 [加入新項目] 對話方塊中,選取 [功能區 (XML)]。

  3. 將新功能區的名稱改成 MyRibbon,然後按一下 [加入]。

    MyRibbon.cs 或 MyRibbon.vb 檔案隨即在設計工具中開啟。 您的專案中也會加入名稱為 MyRibbon.xml 的 XML 檔。

  4. 以滑鼠右鍵按一下 [方案總管] 中的 [ThisAddin.cs] 或 [ThisAddin.vb],然後按一下 [檢視程式碼]。

  5. 將下列程式碼加入至 [ThisAddin] 類別。 此程式碼會覆寫 CreateRibbonExtensibilityObject 方法並將功能區 XML 類別傳回至 Office 應用程式。

    Protected Overrides Function CreateRibbonExtensibilityObject() As  _
    Microsoft.Office.Core.IRibbonExtensibility
        Return New MyRibbon()
    End Function
    
    protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
    {
        return new MyRibbon();
    }
    
  6. 以滑鼠右鍵按一下 [方案總管] 中的 [MyRibbonAddIn] 專案,然後按一下 [建置]。 接著驗證專案建置無誤。

將按鈕加入至增益集索引標籤

此增益集的目標是要為使用者提供一種方法,讓他們能夠將範本文字和特定表格加入至現用文件。 若要提供使用者介面,請修改功能區 XML 檔,以便將兩個按鈕加入至 [增益集] 索引標籤中。 在此逐步解說稍後的內容中,您會定義這些按鈕的回呼方法。 如需功能區 XML 檔的詳細資訊,請參閱功能區 XML

若要將按鈕加入至增益集索引標籤

  1. 以滑鼠右鍵按一下 [方案總管] 中的 [MyRibbon.xml],然後按一下 [開啟]。

  2. 以下列 XML 取代 tab 項目的內容。 這個 XML 會將預設控制項群組的標籤變更為 [Content],而且會加入兩個標籤為 [Insert Text] 和 [Insert Table] 的新按鈕。

    <tab idMso="TabAddIns">
        <group id="ContentGroup" label="Content">
            <button id="textButton" label="Insert Text"
                 screentip="Text" onAction="OnTextButton"
                 supertip="Inserts text at the cursor location."/>
            <button id="tableButton" label="Insert Table"
                 screentip="Table" onAction="OnTableButton"
                 supertip="Inserts a table at the cursor location."/>
        </group>
    </tab>
    

使用按鈕來自動化文件

您必須加入 [Insert Text] 和 [Insert Table] 按鈕的 onAction 回呼方法,以便在使用者按一下這兩個按鈕時執行動作。 如需功能區控制項之回呼方法的詳細資訊,請參閱功能區 XML

若要加入按鈕的回呼方法

  1. 以滑鼠右鍵按一下 [方案總管] 中的 [MyRibbon.cs] 或 [MyRibbon.vb],然後按一下 [開啟]。

  2. 在 MyRibbon.cs 或 MyRibbon.vb 檔的頂端加入下列程式碼。 此程式碼會建立 Microsoft.Office.Interop.Word 命名空間的別名。

    Imports Word = Microsoft.Office.Interop.Word
    
    using Word = Microsoft.Office.Interop.Word;
    
  3. 將下列方法加入至 MyRibbon 類別。 這是 [Insert Text] 按鈕的回呼方法,它會在現用文件中的游標目前所在位置上加入字串。

    Public Sub OnTextButton(ByVal control As Office.IRibbonControl)
        Dim currentRange As Word.Range = Globals.ThisAddIn.Application.Selection.Range
        currentRange.Text = "This text was added by the Ribbon."
    End Sub
    
    public void OnTextButton(Office.IRibbonControl control)
    {
        Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range;
        currentRange.Text = "This text was added by the Ribbon.";
    }
    
  4. 將下列方法加入至 MyRibbon 類別。 這是 [Insert Table] 按鈕的回呼方法,它會在現用文件中的游標目前所在位置上加入表格。

    Public Sub OnTableButton(ByVal control As Office.IRibbonControl)
        Dim missing As Object = System.Type.Missing
    
        Dim currentRange As Word.Range = Globals.ThisAddIn.Application.Selection.Range
        Dim newTable As Word.Table = Globals.ThisAddIn.Application.ActiveDocument.Tables.Add( _
                   currentRange, 3, 4)
    
        ' Get all of the borders except for the diagonal borders.
        Dim borders() As Word.Border = New Word.Border(6) {}
        borders(0) = newTable.Borders(Word.WdBorderType.wdBorderLeft)
        borders(1) = newTable.Borders(Word.WdBorderType.wdBorderRight)
        borders(2) = newTable.Borders(Word.WdBorderType.wdBorderTop)
        borders(3) = newTable.Borders(Word.WdBorderType.wdBorderBottom)
        borders(4) = newTable.Borders(Word.WdBorderType.wdBorderHorizontal)
        borders(5) = newTable.Borders(Word.WdBorderType.wdBorderVertical)
    
        ' Format each of the borders.
        For Each border As Word.Border In borders
            border.LineStyle = Word.WdLineStyle.wdLineStyleSingle
            border.Color = Word.WdColor.wdColorBlue
        Next
    End Sub
    
    public void OnTableButton(Office.IRibbonControl control)
    {
        object missing = System.Type.Missing;
        Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range;
        Word.Table newTable = Globals.ThisAddIn.Application.ActiveDocument.Tables.Add(
        currentRange, 3, 4, ref missing, ref missing);
    
        // Get all of the borders except for the diagonal borders.
        Word.Border[] borders = new Word.Border[6];
        borders[0] = newTable.Borders[Word.WdBorderType.wdBorderLeft];
        borders[1] = newTable.Borders[Word.WdBorderType.wdBorderRight];
        borders[2] = newTable.Borders[Word.WdBorderType.wdBorderTop];
        borders[3] = newTable.Borders[Word.WdBorderType.wdBorderBottom];
        borders[4] = newTable.Borders[Word.WdBorderType.wdBorderHorizontal];
        borders[5] = newTable.Borders[Word.WdBorderType.wdBorderVertical];
    
        // Format each of the borders.
        foreach (Word.Border border in borders)
        {
            border.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
            border.Color = Word.WdColor.wdColorBlue;
        }
    }
    

測試增益集

當您執行專案時,Word 會開啟,而且功能區上會出現名稱為 [增益集] 的索引標籤。 請按一下 [增益集] 索引標籤上的 [Insert Text] 和 [Insert Table] 按鈕來測試程式碼。

若要測試增益集

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

  2. 確認 [增益集] 索引標籤已顯示在功能區上。

  3. 按一下 [增益集] 索引標籤。

  4. 確認 [Content] 群組已顯示在功能區上。

  5. 按一下 [Content] 群組中的 [Insert Text] 按鈕。

    此時便會在文件中的游標目前所在位置上加入字串。

  6. 按一下 [Content] 群組中的 [Insert Table] 按鈕。

    此時便會在文件中的游標目前所在位置上加入表格。

後續步驟

您可以透過下列主題,進一步了解自訂 Office UI 的方式:

  • 自訂不同 Office 應用程式的功能區。 如需支援自訂功能區之應用程式的詳細資訊,請參閱功能區概觀

  • 使用功能區設計工具自訂 Office 應用程式的功能區。 如需詳細資訊,請參閱 功能區設計工具

  • 建立自訂執行窗格。 如需詳細資訊,請參閱 執行窗格概觀

  • 使用 Outlook 表單區域自訂 Microsoft Office Outlook 的 UI。 如需詳細資訊,請參閱 逐步解說:設計 Outlook 表單區域

請參閱

工作

逐步解說:使用功能區設計工具建立自訂的索引標籤

概念

功能區 XML

其他資源

功能區概觀