FirstTime: A few simple questions on creating an Excel Addin / Ribbon. RRS feed

  • Question

  • Hello,

    Thanks for reviewing my thread.  I am just just starting out with creating an Excel Addin so I have few questions.  I am using VS 2010 with Excel 2010.

    1) Which of these do I need to include?

    using Office = Microsoft.Office.Core;
    using Tools = Microsoft.Office.Tools;
    using Excel = Microsoft.Office.Tools.Excel;
    using ExcelExt = Microsoft.Office.Tools.Excel.Extensions;
    using Workbook = Microsoft.Office.Tools.Excel.Workbook;
    using Worksheet = Microsoft.Office.Tools.Excel.Worksheet;

    2) How in general do I get access to the instance of the Application class?

    3) I am trying to creat a Ribbon and so how can I obtain the instance of the Excel Application instances so I can get access to the worksheets inside a myButton_Click(object sender, RibbonControlEventArgs e) event?

    4) Do you know where I can download examples of Excel Addin?

    Many Thanks



    Saturday, April 23, 2011 5:37 PM


  • Hi Peter

    1. Of the "using" statements you list, you certainly can use the first three, although I tend to work with using Excel = Microsoft.Office.Interop.Excel for anything I don't explicitly need the Tools namespace for (sometimes you need the "real Office object" rather than the VSTO extension of the object). And in an add-in it would be rare enough that I'm using an Excel object from the Tools namespace. The fourth could be useful if you're actually going to use the extensions. I personally wouldn't include the last two at the top of any of your classes. These represent objects you'd work with as you need them and you'd rarely address anything from these namespaces without having already defined a specific object.

    The purpose of "using" statements is to provide a "shortcut" to the contents of the namespaces, so that you don't need to always be writing out "Microsoft.Office... when declaring objects. You'd rarely (I can't even think of an example, off the top of my head) need this shortcut when working with Workbook and Worksheet as there are no objects that derive from these. All the objects come from the Excel namespace (either Tools or Interop): Excel.Workbook xlWb = xlApp.Workbooks.Add(//params); Excel.Range xlRng = xlWb.Range(//params); and so on.

    2. Excel.Application xlapp = Globals.ThisAddin.Application;

    3. When you're in an add-in you're going to have only the one instance of the Excel application: the one in which the add-in is loaded (see (2)). This can always be accessed via the Globals keyword.

    4. Not right off-hand...

    Cindy Meister, VSTO/Word MVP
    Sunday, April 24, 2011 5:21 AM