Share via


How to: Add Custom Icons to Toolbar and Menu Items

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Application-level projects

Microsoft Office version

  • Outlook 2003

  • Outlook 2007

For more information, see Features Available by Application and Project Type.

This example adds an icon to a command bar button on a custom menu in Microsoft Office Outlook. The icon is included in the project resources. For more information about project resources, see Adding and Editing Resources (Visual C#) and How to: Add or Remove Resources.

To add custom icons

  1. Add code to the Outlook project's ThisAddIn.vb or ThisAddIn.cs file to create CommandBarPopup and CommandBarButton controls that represent the custom menu and menu command. This code checks to see whether the menu exists. If it does exist, this code removes the menu. It then adds the new menu.

    Private MenuBar As Office.CommandBar
    Private newMenuBar As Office.CommandBarPopup
    Private ButtonOne As Office.CommandBarButton
    Private menuTag As String = "AUniqueName" 
    
    
    Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
        RemoveMenubar()
        AddMenuBar()
    End Sub 
    
    Private Sub AddMenuBar()
        Try
            MenuBar = Me.Application.ActiveExplorer().CommandBars.ActiveMenuBar
            newMenuBar = menuBar.Controls.Add( _
                Office.MsoControlType.msoControlPopup, _
                Temporary:=False)
            If newMenuBar IsNot Nothing Then
                newMenuBar.Caption = "See New Icon"
                newMenuBar.Tag = menuTag
                buttonOne = newMenuBar.Controls.Add( _
                    Office.MsoControlType.msoControlButton, _
                    Before:=1, Temporary:=False)
                With buttonOne
                    .Style = Office.MsoButtonStyle _
                    .msoButtonIconAndCaption
                    .Caption = "New Icon"
                    .FaceId = 65
                    .Tag = "c123"
                    .Picture = getImage()
                End With
                newMenuBar.Visible = True 
            End If 
        Catch Ex As Exception
            MessageBox.Show(Ex.Message)
        End Try 
    End Sub 
    
    Private Sub RemoveMenubar()
        Try 
            ' If the menu already exists, remove it. 
            Dim foundMenu As Office.CommandBarPopup = _
                Me.Application.ActiveExplorer().CommandBars.ActiveMenuBar. _
                FindControl(Office.MsoControlType.msoControlPopup, _
                System.Type.Missing, menuTag, True, True)
            If foundMenu IsNot Nothing Then
                foundMenu.Delete(True)
            End If 
        Catch Ex As Exception
            MessageBox.Show(Ex.Message)
        End Try 
    End Sub
    
    private Office.CommandBar menuBar;
    private Office.CommandBarPopup newMenuBar;
    private Office.CommandBarButton buttonOne;
    private string menuTag = "AUniqueTag";
    
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        RemoveMenubar();
        AddMenuBar();
    }
    
    private void AddMenuBar()
    {
        try
        {
            menuBar = this.Application.ActiveExplorer().CommandBars.ActiveMenuBar;
            newMenuBar = (Office.CommandBarPopup)menuBar.Controls.Add(
                Office.MsoControlType.msoControlPopup, missing,
                missing, missing, false);
            if (newMenuBar != null)
            {
                newMenuBar.Caption = "See New Icon";
                newMenuBar.Tag = menuTag;
                buttonOne = (Office.CommandBarButton)
                    newMenuBar.Controls.
                Add(Office.MsoControlType.msoControlButton, System.
                    Type.Missing, System.Type.Missing, 1, true);
                buttonOne.Style = Office.MsoButtonStyle.
                    msoButtonIconAndCaption;
                buttonOne.Caption = "New Icon";
                buttonOne.FaceId = 65;
                buttonOne.Tag = "c123";
                buttonOne.Picture = getImage();
                newMenuBar.Visible = true;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    
    private void RemoveMenubar()
    {
        // If the menu already exists, remove it. 
        try
        {
            Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)
                this.Application.ActiveExplorer().CommandBars.ActiveMenuBar.
                FindControl(Office.MsoControlType.msoControlPopup,
                System.Type.Missing, menuTag, true, true);
            if (foundMenu != null)
            {
                foundMenu.Delete(true);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    
  2. Create a new class named ConvertImage. This class uses System.Forms.Axhost to convert an Image file to an image type that can be applied to the menu item.

    <Microsoft.VisualStudio.Tools.Applications.Runtime.StartupObjectAttribute(0), _
     Global.System.Security.Permissions.PermissionSetAttribute _
     (Global.System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    Public Class ConvertImage
        Inherits System.Windows.Forms.AxHost
        Public Sub New()
            MyBase.New("59EE46BA-677D-4d20-BF10-8D8067CB8B32")
        End Sub 
    
        Public Shared Function Convert(ByVal Image _
            As System.Drawing.Image) As stdole.IPictureDisp
            Convert = GetIPictureFromPicture(Image)
        End Function 
    End Class
    
    sealed public class ConvertImage : System.Windows.Forms.AxHost
    {
        private ConvertImage()
            : base(null)
        {
        }
    
        public static stdole.IPictureDisp Convert
            (System.Drawing.Image image)
        {
            return (stdole.IPictureDisp)System.
                Windows.Forms.AxHost
                .GetIPictureDispFromPicture(image);
        }
    }
    
  3. Add a method to convert the icon file into an Image file by adding it to an ImageList. This code sends the Image file to the ConvertImage.Convert method you created and then returns the file to the caller.

    Private Function getImage() As stdole.IPictureDisp
        Dim tempImage As stdole.IPictureDisp = Nothing 
        Try 
            Dim newIcon As Icon = My.Resources.Icon1
            Dim newImageList As New ImageList
            newImageList.Images.Add(newIcon)
            tempImage = ConvertImage.Convert(newImageList.Images(0))
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try 
        Return tempImage
    End Function
    
    private stdole.IPictureDisp getImage()
    {
        stdole.IPictureDisp tempImage = null;
        try
        {
            System.Drawing.Icon newIcon = 
                Properties.Resources.Icon1;
    
            ImageList newImageList = new ImageList();
            newImageList.Images.Add(newIcon);
            tempImage = ConvertImage.Convert(newImageList.Images[0]);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return tempImage;
    }
    

Compiling the Code

This example requires:

  • An icon named Icon1 in the project resources.

See Also

Tasks

How to: Add Custom Toolbars and Toolbar Items to Outlook

How to: Add Custom Menus and Menu Items to Outlook

How to: Add or Remove Resources

How to: Specify an Application Icon (Visual Basic, C#)

Concepts

Outlook Object Model Overview

Other Resources

Managing Application Resources