Office Development with Visual Studio

Develop Office Business Applications using Visual Studio

March, 2008

Posts
  • Office Development with Visual Studio

    Developing an add-in for multiple versions of Office (Mary Lee)

    • 9 Comments

    Theoretically, you can develop an add-in for multiple versions of Microsoft Office by catering to the lowest common denominator. This means if you use an Excel 2003 add-in template in Visual Studio 2008, you would be able to develop and debug this with Excel 2007. However if you try this, you may meet these error messages: "You cannot debug or run this project, because the required version of the Microsoft Office application is not installed.", followed by "Unable to start debugging."

    You can develop an Office 2003 add-in but use the 2007 Microsoft Office system for debugging. The following procedure demonstrates how to update your Visual Studio debugging options to use Microsoft Excel 2007 to debug an add-in targetting Microsoft Excel 2003.

    1. On the Project menu, click on ProjectName Properties.

    2. Click on the Debug tab.

    3. In the Start Action pane, click the Start external program radio button.

    4. Click the file browser button and navigate to %ProgramFiles%\Microsoft Office\Office12.

    5. Choose Excel.exe and click Open.

    6. Press F5 to debug your add-in.

    This procedure works because the .NET Framework runtime loads the 2007 Microsoft Office version of the primary interop assembly, even though the add-in was developed with the Microsoft Office 2003 PIA. For more information, see Office Primary Interop Assemblies.

    Before you use a Microsoft Office 2003 template, consider the impact on your project. Because you created your project with an Excel 2003 add-in template, you can now develop with the .NET Framework 2.0. However, this means that you cannot use new features in Visual Studio 2008 that depend on .NET Framework 3.5.

    For additional information, see Andrew Whitechapel's blog entry Can you build one add-in for multiple versions of Office? and the MSDN Library documentation Creating Solutions for Multiple Versions of Microsoft Office.

    Mary Lee, programming writer.

  • Office Development with Visual Studio

    Share a Ribbon Customization between Office Applications (Norm Estabrook)

    • 6 Comments

    We have received feedback from several folks that they would like design one Ribbon by using the Ribbon visual designer, and then re-use that same Ribbon in more than one Office application. This approach makes sense. If my custom tab looks the same for each application, I really don’t want to create the same Ribbon in four separate Office projects. 

     

    So I poked around to see if this was possible. I caught up with one of developers who created the VSTO Ribbon designer and he showed me exactly how to do it.

     

    First, add a Ribbon to a VSTO project. Then, copy the Ribbon code files to a class library project. Finally, add a reference to the class library assembly from within any VSTO project and voila. Well, it’s a tad more challenging.  Here are all the detailed steps.

     

    Create the Ribbon

     

    1.      Create a 2007 Excel, Outlook, PowerPoint, or Word project in Visual Studio. For the purpose of these steps, create a C# project and name the project RibbonStarterProject.

    2.      Add a Ribbon (Visual Designer) item to the project. For the purpose of these steps, accept the default name “Ribbon1”.

    3.      Save and close the project.

     

    Create a Class Library Project

     

    1.      Create a new class library project in Visual Studio. For the purpose of these steps, name the project SharedRibbonLibrary.

    2.      Add a project reference to the Microsoft.Office.Tools.Common.v9.0 assembly.

    3.      On the Project Menu in Visual Studio, click Add Existing Item.

    4.      In the Add Existing Item dialog box, browse to the “RibbonStarterProject” project directory, select the Ribbon.cs file, and click Add.

    Ribbon1.cs is copied to the project directory and appears beneath the project node in Solution Explorer.

    5.      Double-click Ribbon1.cs.

    The Ribbon designer appears.

    6.      From the Office Ribbon Controls tab of the Toolbox, drag a button onto group1.

    7.      Click button1 to select it.

    8.      In the Properties window, set Modifiers to Public.

    Note:  By default, controls that you add to the Ribbon are Internal. That makes them only accessible to code inside the same assembly. However, when you access these controls, you will be accessing them through an assembly reference. Therefore, to reach them from code, you must make them public. More on this soon.

    9.      Right-click the Ribbon designer, and then click Properties.

    10.  In the Properties window, click the RibbonType property, and then select the Ribbon ID’s of the applications or Outlook Inspector windows in which you want the Ribbon to appear. For more information about this property, see the MSDN reference topic for the RibbonType property.

    11.  In Solution Explorer, right-click Ribbon1.cs, and then click View Code.

    12.  Change the namespace of the class to “SharedRibbonLibrary”.

    13.  Repeat this step for the Ribbon1.designer.cs file.

    14.  Compile and save the SharedRibbonLibrary project. You can now use the resulting assembly in any VSTO project that supports the Ribbon.

     

    Consume the Ribbon Customization

     

    1.      Create 2007 Excel, Outlook, PowerPoint, or Word project.

    2.      Add a reference to the SharedRibbonLibrary assembly.

    3.      Add the following code to the ThisAddin, ThisWorkbook, or ThisDocument class of your project. This code overrides the CreateRibbonExtensibilityObject method and returns the Ribbon to the Office application.

     

    protected override Microsoft.Office.Core.IRibbonExtensibility

    CreateRibbonExtensibilityObject()

    {

        return new Microsoft.Office.Tools.Ribbon.RibbonManager(

            new Microsoft.Office.Tools.Ribbon.OfficeRibbon[] { new       

               SharedRibbonLibrary.Ribbon1() });

     

    }

     

    4.      Add a new class to the project. Accept the default name “Class1.cs”.

    5.      Replace the code in the Class1 file with the following:

     

    partial class ThisRibbonCollection : Microsoft.Office.Tools.Ribbon.RibbonReadOnlyCollection

    {

        internal SharedRibbonLibrary.Ribbon1 Ribbon1

        {

            get { return this.GetRibbon<SharedRibbonLibrary.Ribbon1>(); }

        }

    }

    Ok – You are done! You can now access the Ribbon and the button that you added to the Ribbon in your code.  Lets try by handling an event in the consuming project.

     

    Handle the Button Click Event

     

    1.      Add the following code to the startup event handler of project.

     

    Globals.Ribbons.Ribbon1.button1.Click += new EventHandler<Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs>(button1_Click);

     

    2.      Add the following event handler to your project:

     

         void button1_Click(object sender,

    Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e)

    {

        System.Windows.Forms.MessageBox.Show("I can handle events!");

    }

    3.      Run the project.

     

    4.      When the Office application opens, click the Add-Ins tab, and then click your button.

    A message that says “I can handle events!” appears.

     

    - Norm Estabrook

    Programming Writer for BizApps User Education

  • Office Development with Visual Studio

    New Power Tools - Ribbon IDs Tool Window (Kemp Brown)

    • 1 Comments

    The Visual Studio 2008 Tools for Office team just released a cool suite of productivity apps for VS Office development called the VSTO Power Tools. One of the tools included is the Ribbon IDs Tool Window, an add-in tool window for VS that lets you browse the MSO icons that come with Office. You can use these icons to spiff up your custom Ribbon controls. All you need to do is click an icon in the Ribbon IDs Tool Window to obtain its ID, and then plug the value into the control's OfficeImageID property.

    I've recorded a short video that demonstrates the tool window and how to use it to customize some Ribbon controls:

    There are a couple of additional items not in the video that bear mentioning:

    1. You can also use this tool with XML-based ribbons, even dragging-and-dropping the icon value into the XML.
    2. You are not limited to using only MSO images in your controls, although that this is the purpose of this tool.

    Here are links to the Help topics I mentioned at the end of the video:

    Try it out and let us know what you think!

    - Kemp Brown
    Programming Writer for BizApps User Education

Page 1 of 1 (3 items)