Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / IIS

VSTO 2005 add-ins and Outlook 2003

1.06/5 (10 votes)
2 Jul 2009CPOL1 min read 1   244  
Adding a command bar to Outlook 2003.

Introduction

VSTO 2005 enables you to create document level solutions in managed code (C# and VB) for Word and Excel. Document level solutions are a little different than Word or Excel add-ins, which are application scope. VSTO solutions are tied to a document, and the lifetime of the solution is that of the document. When you open the document, your code is loaded and run. And, when you close the document, your code is unloaded. A new feature that was added for Beta2 is the ability to create managed application-level add-ins for Outlook.

Background

The IDTExensibility2 interface is the core concept behind Office add-ins.

A COM add-in is an in-process COM server, or ActiveX dynamic link library (DLL), that implements the IDTExensibility2 interface as described in the Microsoft Add-in Designer type library (Msaddndr.dll). All COM add-ins inherit from this interface and must implement each of its five methods.

Using the code

The code contains two major parts:

  1. ThisAddIn_Startup
  2. Startup is raised after the document is running and all the initialization code in the assembly has been run. It is the last thing to run in the constructor of the class that your code is running in.

  3. ThisAddIn_Shutdown
  4. Shutdown is raised for each of the host items (document or worksheets) when the application domain that your code is loaded in is about to unload. It is the last thing to be called in the class as it unloads.

C#
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    try
    {
        Office.CommandBars cmdBars = 
           Application.ActiveExplorer().CommandBars;
        Office.CommandBar cmdBar = cmdBars["Standard"];

        try
        {
            myExtractButton = (Office.CommandBarButton)
                               cmdBar.Controls<btnextracttag>;
        }
        catch (Exception)
        {
            myExtractButton = null;
        }

        if (myExtractButton == null)
        {
            myExtractButton = (Office.CommandBarButton)
              cmdBar.Controls.Add(1,missing ,missing , 
              missing ,missing );
            myExtractButton.Style = 
              Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;
            myExtractButton.Picture =getImage();
            myExtractButton.FaceId = 2521;
            myExtractButton.Caption = btnExtractTag;
            myExtractButton.Tag = btnExtractTag;
        }

        //event
        myExtractButton.Click += 
          new Microsoft.Office.Core.
          _CommandBarButtonEvents_ClickEventHandler(myExtractButton_Click);

    }
    catch (Exception ex)
    {
        MessageBox.Show("Error initializing sample addin:\r\n" + 
                        ex.Message, "Error", MessageBoxButtons.OK, 
                        MessageBoxIcon.Error);
    }
}

void myExtractButton_Click(
     Microsoft.Office.Core.CommandBarButton Ctrl, 
     ref bool CancelDefault)
{
    Outlook.MAPIFolder folder = Application.Session.PickFolder();
}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}

Adding icons to the command button

Include the class below:

C#
class MyHost : System.Windows.Forms.AxHost
{

    public MyHost(): base(null)
    //("59EE46BA-677D-4d20-BF10-8D8067CB8B33")
    { 
    }

    public static stdole.IPictureDisp Convert(
                  System.Drawing.Image image)
    {
        return (stdole.IPictureDisp)
          System.Windows.Forms.AxHost.GetIPictureDispFromPicture(image);
    }

    private stdole.IPictureDisp getImage()
    {
        stdole.IPictureDisp tempImage = null;
        try
        {
            System.Drawing.Icon newIcon =Properties.Resources.Icon1 ;
            System.Windows.Forms.ImageList imgList = 
                new System.Windows.Forms.ImageList();
            imgList.Images.Add(newIcon);
            tempImage = MyHost.Convert(imgList.Images[0]);
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.ToString());
        }
        return tempImage;
    }
}

Note: Add your icon file in the resource folder.

More reference

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)