Have You ever wondered how to create AddIns for Microsoft Office Applications? In this tutorial I’ll show You how to create an Microsoft Office Excel 2007(although, I think there’s no difference with 2010) AddIn with ribbon  and custom task panes with variuos funcionality. This post, as Part 1 of the tutorial, will describe how to create a solution and ribbon with one button.

1. Create new solution with VS 2010

2. In the Object Explorer You should see:

3. Create a new folder in the solution and name it Ribbon. Create a new .cs file and .XML in this folder.

4. Open .xml file and enter below XML structure, to create aribbon with one button:


<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="OnLoad">
 <ribbon>
 <tabs>
 <tab idMso="TabAddIns">
 <group id="AlertsGroup"
 label="Alerts">
 <button id="btnDisplayAlert"
 size="large"
 label="Display Alert"
 screentip="My Alerts"
 onAction="btnDisplayAlert_OnAction"
 imageMso="AnimationAudio" />
 </group>
 </tab>
 </tabs>
 </ribbon>
</customUI>

5. Select the .xml file and Select the Embedded Resource in Build Action

6. Edit the .cs file


[ComVisible(true)]
 public class zzRibbon : Microsoft.Office.Core.IRibbonExtensibility
 {
 private Microsoft.Office.Core.IRibbonUI _ribbon;

 public zzRibbon()
 {
 }

 #region IRibbonExtensibility Members

 //Returns the ribbon xml content to render it
 public string GetCustomUI(string ribbonID)
 {
 Assembly asm = Assembly.GetExecutingAssembly();
 Stream resource = asm.GetManifestResourceStream("zavaz.ExcelAddIn.Ribbon.zzRibbon.xml");

 if (resource == null)
 return null;

 resource.Position = 0;
 using (StreamReader reader = new StreamReader(resource))
 {
 return reader.ReadToEnd();
 }
 }

 #endregion

 public void OnLoad(Microsoft.Office.Core.IRibbonUI ribbon)
 {
 _ribbon = ribbon;
 }

 public void btnDisplayAlert_OnAction(Microsoft.Office.Core.IRibbonControl control)
 {
 MessageBox.Show("AddIn ALERT!");
 }
 }

7. Edit ThisAddIn.cs – add th Your ribbon object declaration and override the RequestService method


public partial class ThisAddIn
 {
 zzRibbon Ribbon;

 private void ThisAddIn_Startup(object sender, System.EventArgs e)
 {
 }

 private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
 {
 }

 protected override object RequestService(Guid serviceGuid)
 {
 if (serviceGuid == typeof(Office.IRibbonExtensibility).GUID)
 {
 if (Ribbon == null)
 Ribbon = new zzRibbon();
 return Ribbon;
 }

 return base.RequestService(serviceGuid);
 }

 #region VSTO generated code

 /// <summary>
 /// Required method for Designer support - do not modify
 /// the contents of this method with the code editor.
 /// </summary>
 private void InternalStartup()
 {
 this.Startup += new System.EventHandler(ThisAddIn_Startup);
 this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
 }

 #endregion
 }

8. Try to build Your solution and then press F5. Excel 2007 instance should appear with a new Ribbon “AddIns” tab and Your button in it. When You click the button a message box with alert should appear.

That’s it for Part 1 of this tutorial. In the Part 2 I’ll talk about custom panes and how to modify cell values.