Click here to Skip to main content
15,893,904 members
Articles / Web Development / IIS

VSTO 2005 add-ins and Outlook 2003

Rate me:
Please Sign up or sign in to vote.
1.06/5 (10 votes)
2 Jul 2009CPOL1 min read 41.2K   244   18   6
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)


Written By
Web Developer
India India
visit www.renjur.co.nr
RENJUCOOL
RENJUR

Comments and Discussions

 
GeneralMy vote of 2 Pin
Lalit K Chaudhary13-May-10 4:31
Lalit K Chaudhary13-May-10 4:31 
GeneralFile is wrong Pin
Member 289604222-Dec-09 20:58
Member 289604222-Dec-09 20:58 
GeneralMy vote of 1 Pin
bolivar1232-Jul-09 2:18
bolivar1232-Jul-09 2:18 
If I was a total novice, I'd be asking what is VSTO? Plus where's the article?
GeneralMy vote of 1 Pin
Prajnan Das17-Apr-09 1:29
professionalPrajnan Das17-Apr-09 1:29 
QuestionDude wheres my article? Pin
stensones14-Jun-07 1:55
stensones14-Jun-07 1:55 
AnswerRe: Dude wheres my article? Pin
RENJUR14-Jun-07 18:38
RENJUR14-Jun-07 18:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.