Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Images with Ribbons (XML)

I'm making Office 2007 addins and I'm trying to use XML instead of the visual designer to customize the ribbon but for some reason I cant get an image to work with it... What exactly do you have to do‽ I added a resource png called Icon1 and tried this:

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="Ribbon_Load">
      <ribbon startFromScratch="false">
        <tabs>
          <tab id="TabToolss" label="Tools">
            <group id="MyGroup" label="My Group" visible="true">
              <button
                id="Button1"
                image="WordAddIn1.Properties.Resources.Icon1"
                onAction="Button1_Click"
                showImage="true"
                />
            </group>
          </tab>
        </tabs>
      </ribbon>
    </customUI>

No luck... not sure why. Even if I put the full file path in there it doesn't work.

I've never gotten it to work once, so maybe I'm just not doing it the way it was made to be done...

like image 211
Mark Lalor Avatar asked Aug 21 '11 05:08

Mark Lalor


2 Answers

Very complicated solution... lucky to find it here

First add loadImage attribute to CustomUI tab,

Then to simplify things, add this internal class

internal class PictureConverter : AxHost
    {
        private PictureConverter() : base(String.Empty) { }

        static public stdole.IPictureDisp ImageToPictureDisp(Image image)
        {
            return (stdole.IPictureDisp)GetIPictureDispFromPicture(image);
        }

        static public stdole.IPictureDisp IconToPictureDisp(Icon icon)
        {
            return ImageToPictureDisp(icon.ToBitmap());
        }

        static public Image PictureDispToImage(stdole.IPictureDisp picture)
        {
            return GetPictureFromIPicture(picture);
        }
    }

Next add definition of loadImage function,

public IPictureDisp Ribbon_LoadImage(string imageName)
        {
            return PictureConverter.ImageToPictureDisp((Bitmap)Resources.ResourceManager.GetObject(imageName));
        }
like image 182
Mark Lalor Avatar answered Oct 13 '22 00:10

Mark Lalor


its a lot easier if you do the following code:

<?xml version="1.0" encoding="UTF-8"?> 
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"onLoad="Ribbon_Load>
 <ribbon startFromScratch="false">
   <tabs>
     <tab id="TabToolss" label="Tools">
      <group id="MyGroup" label="My Group" visible="true">
       <button id="Button1" size="normal" getImage="Icon1"
        onAction="Button1_Click"
        showImage="true"/>
    </group>
  </tab>
 </tabs>
</ribbon>

in the Ribbon cs:

namespace Add_in
{
[ComVisible(true)]
public class MyRibbon : Office.IRibbonExtensibility
{
    private Office.IRibbonUI ribbon;

    public MyRibbon()
    {

    }

    public Bitmap Icon1(Office.IRibbonControl control)
    {
        return (Bitmap)Properties.Resources.ResourceManager.GetObject("Icon1");
    }
like image 21
Jil Avatar answered Oct 12 '22 22:10

Jil