Skip to content

Commit 4128932

Browse files
committed
Ribbon: Add the ribbon controller
1 parent d33b970 commit 4128932

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

Ribbon/Readme.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Ribbon Sample
22

3+
This sample shows how to add a ribbon UI extension with the Excel-DNA add-in, and how to use the Excel COM object model from C# to write some information to a workbook.
4+
35
## Initial setup
46

57
1. Create new Class Library project.
@@ -20,3 +22,84 @@ namespace Ribbon
2022
```
2123

2224
4. Press F5 to load in Excel, and then test `=dnaRibbonTest()` in a cell.
25+
26+
## Add the ribbon controller
27+
28+
1. Add a reference to the `System.Windows.Forms` assembly (we'll use that for showing our messages).
29+
30+
2. Add a new class for the ribbon controller (maybe `RibbonController.cs`), with this code for a button and handler:
31+
32+
```cs
33+
using System.Runtime.InteropServices;
34+
using System.Windows.Forms;
35+
using ExcelDna.Integration.CustomUI;
36+
37+
namespace Ribbon
38+
{
39+
[ComVisible(true)]
40+
public class RibbonController : ExcelRibbon
41+
{
42+
public override string GetCustomUI(string RibbonID)
43+
{
44+
return @"
45+
<customUI xmlns='http://schemas.microsoft.com/office/2006/01/customui'>
46+
<ribbon>
47+
<tabs>
48+
<tab id='tab1' label='My Tab'>
49+
<group id='group1' label='My Group'>
50+
<button id='button1' label='My Button' onAction='OnButtonPressed'/>
51+
</group >
52+
</tab>
53+
</tabs>
54+
</ribbon>
55+
</customUI>";
56+
}
57+
58+
public void OnButtonPressed(IRibbonControl control)
59+
{
60+
MessageBox.Show("Hello from control " + control.Id);
61+
}
62+
}
63+
}
64+
```
65+
66+
3. Press F5 to load and test.
67+
68+
69+
### Notes
70+
71+
* The ribbon class derives from the `ExcelDna.Integration.CustomUI.ExcelRibbon` base class. This is how Excel-DNA itentifies the class a defining a ribbon controller.
72+
73+
* The ribbon class must be 'COM visible'. Either the class must be marked as `[ComVisible(true)]` (the default class library template in Visual Studio markes the assembly as `[assembly:ComVisible(false)]`).
74+
75+
* The xml namespace is important. Excel 2007 introduced the ribbon, and support only the original xml namespace as shown in this example - `xmlns='http://schemas.microsoft.com/office/2006/01/customui'`. Further enhancements to the ribbon was made in Excel 2010, including using the ribbon for worksheet context menus and adding the backstage area. To indicate the extended Excel 2010 xml schema, this version and later supports an update namespace - `xmlns='http://schemas.microsoft.com/office/2009/07/customui'`.
76+
77+
* The Office applications have a debugging setting to assist in finding any errors in the ribbon xml, which would prevent the ribbon from loading. In Excel 2013, this setting can be found under `File -> Options -> Advanced`, then under `General` find 'Show add-in user interface errors'. Note that this setting applied to all installed Office applications, and can reveal unexpected errors that are present in other add-ins too.
78+
79+
* There are different options for providing the ribbon xml. In this sample it is embedded as a string in the code and returned from the `ExcelRibbon.GetCustomUI` overload. Excel-DNA also supports placing the xml inside the .dna add-in configuration file (this is where the base class implementation of `GetCustomUI` looks for it). The ribbon xml can also be put in an assembly resource (either as a string or from a separate file) and extracted at runtime with some extra code in `GetCustomUI`.
80+
81+
* The callback methods, like `OnButtonPressed` in the example, are found by Excel using the COM `IDispatch` interface that is implicitly implemented by the COM visible .NET class.
82+
83+
* Behind the scenes, Excel-DNA registers and loads a COM helper add-in that provides the ribbon support. This COM helper add-in should load even if the user does not have administrator rights, but it might be blocked by some Excel-specific security settings.
84+
85+
* Errors in the ribbon methods can cause Excel to mark the ribbon COM helper add-in as a 'Disabled Add-in'. This will reflect in the 'Disabled Add-ins' list under `File-> Options -> Add-Ins` under the `Manage` dropdown.
86+
87+
### Ribbon xml and callback documentation
88+
89+
Excel-DNA is responsible for loading the ribbon helper add-in, but is not otherwise involved in the ribbon extension. This means that the custom UI xml schema, and the signatures for the callback methods are exactly as documented by Microsoft. The best documentation for these aspects can be found in the three-part series on 'Customizing the 2007 Office Fluent Ribbon for Developers':
90+
91+
* [Part 1 - Overview](https://msdn.microsoft.com/en-us/library/aa338202.aspx)
92+
* [Part 2 - Controls and callback reference](https://msdn.microsoft.com/en-us/library/aa338199.aspx)
93+
* [Part 3 - Frequently asked questions, including C# and VB.NET callback signatures](https://msdn.microsoft.com/en-us/library/aa722523.aspx)
94+
95+
Information related to the Excel 2010 extensions to the ribbon can be found here:
96+
97+
* [Customizing Context Menus in Office 2010](https://msdn.microsoft.com/en-us/library/office/ee691832.aspx)
98+
* [Customizing the Office 2010 Backstage View](https://msdn.microsoft.com/en-us/library/office/ee815851.aspx)
99+
* [Ribbon Extensibility in Office 2010: Tab Activation and Auto-Scaling](https://msdn.microsoft.com/en-us/library/office/ee691834.aspx)
100+
101+
Other ribbon-related resources:
102+
103+
* [Ron de Bruin's Excel Tips](http://www.rondebruin.nl/win/s2/win003.htm)
104+
* [Andy Pope's RibbonX Visual Designer](http://www.andypope.info/vba/ribboneditor.htm)
105+

Ribbon/Ribbon.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
</Reference>
3737
<Reference Include="System" />
3838
<Reference Include="System.Core" />
39+
<Reference Include="System.Windows.Forms" />
3940
<Reference Include="System.Xml.Linq" />
4041
<Reference Include="System.Data.DataSetExtensions" />
4142
<Reference Include="Microsoft.CSharp" />
@@ -45,6 +46,7 @@
4546
<ItemGroup>
4647
<Compile Include="Functions.cs" />
4748
<Compile Include="Properties\AssemblyInfo.cs" />
49+
<Compile Include="RibbonController.cs" />
4850
</ItemGroup>
4951
<ItemGroup>
5052
<None Include="Ribbon-AddIn.dna" />

Ribbon/RibbonController.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Runtime.InteropServices;
2+
using System.Windows.Forms;
3+
using ExcelDna.Integration.CustomUI;
4+
5+
namespace Ribbon
6+
{
7+
[ComVisible(true)]
8+
public class RibbonController : ExcelRibbon
9+
{
10+
public override string GetCustomUI(string RibbonID)
11+
{
12+
return @"
13+
<customUI xmlns='http://schemas.microsoft.com/office/2006/01/customui' loadImage='LoadImage'>
14+
<ribbon>
15+
<tabs>
16+
<tab id='tab1' label='My Tab'>
17+
<group id='group1' label='My Group'>
18+
<button id='button1' label='My Button' onAction='OnButtonPressed'/>
19+
</group >
20+
</tab>
21+
</tabs>
22+
</ribbon>
23+
</customUI>";
24+
}
25+
26+
public void OnButtonPressed(IRibbonControl control)
27+
{
28+
MessageBox.Show("Hello from control " + control.Id);
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)