0
Answer

Programatically Load Excel Ribbon using c# .Net 4.0

chetana rupa

chetana rupa

12y
4.8k
1
Hi

Is it possible to load a ribbon on excel programatically at run time using .Net c# ?

I dont want to use any Template project or Addin project or Workbook 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 similiar lines. For this I tryed in the following way. I am not getting any error but ribbon is not shown on excel

code sample:

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;

          }

    }