Click here to Skip to main content
15,065,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Is it possible to load a ribbon on excel programatically at run time using .Net 4.0 c# ?

I don't want to use any add-in project or any excel template project to do this. I just want to build a class library which shall trigger on button click of windows from project.

For example the following link demonstrates how to automate Excel to create a command bar that contains buttons, drop-down list boxes, combo boxes, and pop-up menus.

http://support.microsoft.com/kb/303018

I Want to load ribbon on similar lines.

I tried as following : I am not getting any error but ribbon is not shown on excel

code sample:
C#
using Ribbons = Microsoft.Office.Tools.Ribbon;

 public class loadribboncls
    {
        Excel.Workbook _XLWorkBook;
        Object MissVal = System.Reflection.Missing.Value;
        string path;
        Excel.Application oExcel; 
        public string Path
        {
            get { return path; }
            set { path = value; }
        }
        
        public Excel.Application applicationObject
        {
            get { return oExcel; }
            set { oExcel = value; }
        }       

        public void loadRibbon()
        {
            applicationObject = new Excel.Application();
            _XLWorkBook = applicationObject.Workbooks.Open(Path, MissVal, MissVal, MissVal,
                                                                    MissVal, MissVal, MissVal,
                                                                    MissVal, MissVal, MissVal, MissVal,
                                                                    MissVal, MissVal, MissVal, MissVal);
            _XLWorkBook = (Excel.Workbook)Marshal.BindToMoniker(Path);            
            applicationObject.Visible = true;


            Ribbons.RibbonTab ribbontab = new Ribbons.RibbonTab();
            ribbontab.ControlId.ControlIdType = Ribbons.RibbonControlIdType.Office;
            ribbontab.ControlId.OfficeId = "SW Ribbon";

            Ribbons.RibbonGroup ribbongroup = new Ribbons.RibbonGroup();
            Ribbons.RibbonButton ribbonbutton1 = new Ribbons.RibbonButton();
            ribbonbutton1.Name = "1 btn";
            ribbonbutton1.Label = "My Sample Button 1";


            Ribbons.RibbonButton ribbonbutton2 = new Ribbons.RibbonButton();
            ribbonbutton2.Name = "2 btn";
            ribbonbutton2.Label = "My Sample Button 2";


            ribbongroup.Items.Add(ribbonbutton1);
            ribbongroup.Items.Add(ribbonbutton2);
            ribbontab.Groups.Add(ribbongroup);

            ribbontab.Visible = true;
            ribbongroup.Visible = true;

          }

    } 

--------------------------------------------------------------------------------
Chetana
Posted
Updated 14-Dec-11 19:47pm
v2
Comments
BillWoodruff 18-Dec-11 10:40am
   
Does MS Excel actually allow multiple "Ribbons" ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900