Exercise 3: Using the Visual Studio 2010 Ribbon Designer

In this exercise you will use the Ribbon designer in Visual Studio 2010 to add a custom ribbon to Word 2010.

Task 1 – Create a new Ribbon

In this task, you will create a new Publishing ribbon using the Ribbon designer.

  1. Add a new Ribbon item to the project
    1. In the Solution Explorer, right click Word2010AddIn and click Add –> New Item
    2. Select the Office template on the left hand side
    3. Choose an item of type Ribbon (Visual Designer)
    4. Name the new item Ribbon.cs(Ribbon.vb in case of VB) and click Add

      Figure 8(a)

      Figure 8(b)

      Add New Ribbon Dialog

  2. Rename the ribbon tab and group in the designer
    1. Right click the tab in the designer and select Properties
    2. In the Properties window set the Label to Publishing and the (Name) to tabPublishing
    3. In the designer, select the group named group1
    4. In the Properties window set the Label to Formats and the (Name) to grpFormats
  3. Add a PDF button to the Formats group
    1. Drag a button from the Toolbox into the Formats group

      Note:
      If the Toolbox is not visible select View -> Toolbox in the menu

    2. In the Properties window set the Label to PDF and the (Name) to btnPDF
    3. Set the ControlSize to RibbonControlSizeLarge
    4. Click the button in the Image property
    5. In the Select Resource dialog, select Local resource and click Import
    6. Choose %Office2010DeveloperTrainingKitPath%\Labs\GettingStarted\Source\[language]\Starter\pdf.png
    7. Click OK to select the image

      Figure 9

      PDF Button in the Designer

  4. Add an XPS button to the Formats group
    1. Use the same procedure as above except use XPS in the Label and (Name) and choose xps.png instead of pdf.png

      Figure 10

      PDF and XPS Button in the Designer

  5. Add code to publish the document to both buttons
    1. Double click the PDF button in the designer to generate a handler in code
    2. Switch back to the designer and double click the XPS button to generate a second handler
    3. In the code, enter the following using statements

      C#

      using System.IO; using Word = Microsoft.Office.Interop.Word;

      Visual Basic

      Imports System.IO Imports Word = Microsoft.Office.Interop.Word

    4. In the btnPDF_Click method use the following code to export the current document to the desktop as a PDF.

      C#

      string desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string fileName = "QuickExport.pdf"; Globals.ThisAddIn.Application.ActiveDocument.ExportAsFixedFormat( Path.Combine(desktopFolder, fileName), Word.WdExportFormat.wdExportFormatPDF, OpenAfterExport: true);

      Visual Basic

      Dim desktopFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) Dim fileName As String = "QuickExport.pdf" Globals.ThisAddIn.Application.ActiveDocument.ExportAsFixedFormat(Path.Combine(desktopFolder, fileName), Word.WdExportFormat.wdExportFormatPDF, OpenAfterExport:= True)

    5. Use this code in btnXPS_Click to export the current document to the desktop as an XPS.

      C#

      string desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string fileName = "QuickExport.xps"; Globals.ThisAddIn.Application.ActiveDocument.ExportAsFixedFormat( Path.Combine(desktopFolder, fileName), Word.WdExportFormat.wdExportFormatXPS, OpenAfterExport: true);

      Visual Basic

      Dim desktopFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) Dim fileName As String = "QuickExport.xps" Globals.ThisAddIn.Application.ActiveDocument.ExportAsFixedFormat(Path.Combine(desktopFolder, fileName), Word.WdExportFormat.wdExportFormatXPS, OpenAfterExport:= True)
      Note:
      The ExportAsFixedFormat method has many parameters not provided. C#\VB.Net 4.0 requires only the necessary parameters.

Exercise 3 Verification

In order to verify that you have correctly performed all steps in the above exercise, proceed as follows:

Test the Add-in

Test your add-in by creating a new document and enter some text. Once you are ready to publish your document, switch to the Publishing tab and choose export to PDF or XPS. If a PDF or XPS file appears on the desktop, your add-in worked.

  1. From the Debug menu, choose Start Debugging.
  2. Once Word 2010 has loaded and you have dismissed the message box, enter some content into the document.

    Figure 11

    Custom Publishing Ribbon

  3. Switch to the Publishing ribbon tab
  4. Click the PDF and the XPS buttons to export the documents in both formats
  5. When the files load in their respective viewers, verify the contents are accurate

    Figure 12

    XPS Viewer

  6. When you are done cleanup and remove the add-in
    1. Close Word 2010
    2. In the Solution Explorer, right click Word2010AddIn and click Clean