Ribbon Image in SDK-Style Projects

71 views
Skip to first unread message

Ricardo Gerbaudo

unread,
Sep 20, 2022, 12:38:55 PM9/20/22
to Excel-DNA
Hi all, I'm trying to follow the Ribbon tutorials but I don't know where to specify the <Image> element in the .csproj file, since there's no .dna file in SDK-style projects. Any tips?

Project File:
<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net48</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="ExcelDna.AddIn" Version="1.6.0-rc1" />
        <PackageReference Include="ExcelDna.Interop" Version="15.0.0" />
        <PackageReference Include="ExcelDna.XmlSchemas" Version="1.0.0" />
    </ItemGroup>

    <PropertyGroup>
        <ExcelDnaAllowPackageReferenceProjectStyle>true</ExcelDnaAllowPackageReferenceProjectStyle>
        <ExcelDna>
            <Image Name="MyPicture" Path="MyPicture.png" Pack="true"/>
        </ExcelDna>
    </PropertyGroup>

</Project>



Ribbon Class:
using ExcelDna.Integration;
using ExcelDna.Integration.CustomUI;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using Application = Microsoft.Office.Interop.Excel.Application;

namespace ModelValidation.AddIn
{
    [ComVisible(true)]
    public class Ribbon : ExcelRibbon
    {
        public override string GetCustomUI(string RibbonID)
        {
            return @"
                <customUI xmlns='http://schemas.microsoft.com/office/2009/07/customui' loadImage='LoadImage'>
                    <ribbon>
                        <tabs>
                            <tab id='tab1' label='My Tab'>
                                <group id='group1' label='My Group'>
                                    <button id='button1' label='Say Hello' onAction='OnSayHelloPressed' image='MyPicture'/>
                                </group>
                            </tab>
                        </tabs>
                    </ribbon>
                </customUI>";
        }
       
        public void OnSayHelloPressed(IRibbonControl control)
        {
            Application app = (Application)ExcelDnaUtil.Application;
            Range range = app.Range["A1"];
            range.Value = "Hello from .NET!";
           
        }
    }
}




Govert van Drimmelen

unread,
Sep 21, 2022, 8:52:23 AM9/21/22
to Excel-DNA
Hi Ricardo,

The move to SDK-style projects needs a lot more examples and documentation, and this is a good question.

Currently we don't have a way to automatically add the images into the generated .dna file, when using the SDK-style project files.
There are two options:

* Manually add a .dna file into your project. This file should then be detected by the Excel-DNA build and used as the basis for the created add-ins. Then everything works like in the world with old-style project files. You can take one of the .dna files currently in your output directory as a starting point.

* Implement the LoadImage yourself. For this you would add the images as resources into your own .dll project. Then implement a callback that extracts the image from the resources, and set this as the callback in the "loadImage" attribute of your ribbon markup.
The signature for the callback would be:

        public object MyLoadImage(string imageId) { ... }

and you can return an object of type System.Drawing.Bitmap.
Putting the resource in your C# library and extracting at runtime is quite standard and you'll find code examples easily.

-Govert

Joachim Hussong

unread,
Oct 5, 2022, 8:21:28 AM10/5/22
to Excel-DNA
Hi folks,

how is this if your are not using Winforms but WPF? In WPF there is no "Bitmap"

It was not a problem to embed images in my ExcelDNA-Winform-Ribbon project, but I am a little stuck with WPF. This is roughly about how I tried it so far (in several variations, ...)

public BitmapImage GetImage(IRibbonControl control)
{
     BitmapImage img = new();
     using (var s = new MemoryStream(Properties.Resources.dialog_ok_2))
     {
          img.BeginInit();
          img.CacheOption = BitmapCacheOption.OnLoad;
          img.StreamSource = s;
          //img.UriSource = new Uri("Resources/dialog-ok2.png", UriKind.Relative);
          img.EndInit();
     }
     return img;
}

...

But there are no icons in my Ribbon (they are loaded correctly and the returned BitmapImage is a filled with the right data), even if there is no direct related Error message about. But I encounter some other strange things like already disposed dll which may be a side effect. Using no Ribbon Icons leads to a perfectly running project.

Some hint is appreciated.

J.

Govert van Drimmelen

unread,
Oct 5, 2022, 8:26:33 AM10/5/22
to exce...@googlegroups.com
Hi Joachim,

It doesn't matter whether your forms are WPF or WinForms, you should return the System.Drawing.Bitmap type from that ribbon callback. It implements the right COM interface that Office is looking for.

-Govert

--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/8dcc8a40-3c93-4878-81ae-2cac00dd5dbbn%40googlegroups.com.

Joachim Hussong

unread,
Oct 6, 2022, 4:40:37 AM10/6/22
to exce...@googlegroups.com
Hi Govert,

the issue is that System.Drawing does not contain a "Bitmap" in WPF. System.Drawing.Bitmap is only Winforms.

2022-10-06 10_37_42-DokumentenManager - Microsoft Visual Studio.png

Govert van Drimmelen

unread,
Oct 6, 2022, 5:39:22 AM10/6/22
to exce...@googlegroups.com

Hi Joachim,

 

You just need to add a reference to the System.Windows.Forms assembly to make it available.

That adds no overhead to your add-in, since Excel-DNA loads and uses this assembly anyway.

This will not interfere with your WPF forms either.

 

-Govert

 

 

From: 'Joachim Hussong' via Excel-DNA <exce...@googlegroups.com>
Sent: 6 October 2022 10:40
To: exce...@googlegroups.com
Subject: Re: [ExcelDna] Re: Ribbon Image in SDK-Style Projects

 

Hi Govert,

 

the issue is that System.Drawing does not contain a "Bitmap" in WPF. System.Drawing.Bitmap is only Winforms.

 

image001.png

Joachim Hussong

unread,
Oct 6, 2022, 9:15:51 AM10/6/22
to exce...@googlegroups.com
Thanx for that information. Good to know!


Joachim

Reply all
Reply to author
Forward
0 new messages