Dynamically load ribbon based on .dll

91 views
Skip to first unread message

Praveen Kumar

unread,
Jun 12, 2023, 6:35:05 AM6/12/23
to Excel-DNA
Hello Group,

I recently added a custom ribbon using c# to my projects and its all working fine.
Now the issue is I have multiple projects under the same solution which has different ribbon options and I need to load each project based on the file user is selecting.

I have made some changes to the projects so that they load according to the file selected, but the ribbon menu is static and is not changing accordingly. 

do have any solution?

Kedar Kulkarni

unread,
Jun 12, 2023, 12:31:07 PM6/12/23
to Excel-DNA
I would do following -

Create a single (mega) ribbon with all options and every tab/button has a getEnabled and getVisible option to either show or hide the option based on code that would detect if the project is loaded. 

You could also create dynamic meu as well and add items at runtime. 

If you have multiple xlls (which IMO is a not a good design) you could load in a single ribbon by creating /sharing a namespace across ribbons and they would show under one ribbon. 

Praveen Kumar

unread,
Jun 13, 2023, 4:33:11 AM6/13/23
to Excel-DNA
Thank you Kedar for your reply. 
I tried this too.. but as the click events of the buttons (which are in the ribbon) will be known only at run time and they are all not same for all the projects.

for example, if there is ButtonA for project1, it has one functionality and ButtonB for project2 will have different functionality. (I have around 20 such projects). If I provide an interface or abstract class for child properties to implement and call them at runtime, I will end up implementing these methods for the projects which dont even use them.

Kedar Kulkarni

unread,
Jun 13, 2023, 11:55:35 AM6/13/23
to Excel-DNA
I am unsure what you mean. If a project has a button in ribbon then implementation is necessary. If no ribbon button, there is no need to change the project. 

It entirely depends on what type of project structure you have. eg I could write the implementation inside the tag of ribbon which would have the run time type and method etc without creating an interface and just execute method at runtime but it could be a bad design. 

It could be a config file as well.. there are many ways to implement - I usually just have ribbonHandler a single method that uses ribbon ID and it could then lookup a config file to load appropriate assembly and create a instance of type and execute the method inside.. 

Praveen Kumar

unread,
Jun 13, 2023, 12:19:18 PM6/13/23
to Excel-DNA
just to be clear, this is my project struture.

ProjectMain:
    -> RibbonController.cs   (has GetcustomUI() and returns xml with multiple buttons, not dynamic ones though, and OnAction events for buttons inside ribbon)
   -> ProjectHandler.cs    (loads one of the projects dynamically based on selected workbook)
   
Project A:
    -> some function to be called which shows a form and other stuff

Project B:
    -> different set of functionalities etc


what I tried:
1. created interface in ProjectMain and asked Project A and B to implement with some common functionality.
2. Called those interface functions in ribboncontroller events
3. loaded projects dynamically and its all working fine.

Issue: 
as each project is not same, B has additional functionality A doesnt required and as per my model, A needs to implement my interface even its not required.

Is it possible to  have ribbon and functionality of buttons managed in Projects A and B itself and from ProjectMain I can simply load those ribbons?

(same question incase of any excel functions are there in project A and B)
Reply all
Reply to author
Forward
0 new messages