Click here to Skip to main content
Licence CPOL
First Posted 13 Jun 2007
Views 20,993
Downloads 125
Bookmarked 18 times

VSTO 2005 add-ins and Outlook 2003

By | 2 Jul 2009 | Article
Adding a command bar to Outlook 2003.
 
Part of The SQL Zone sponsored by
See Also

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.

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:

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)

About the Author

RENJUR

Web Developer

India India

Member

visit www.renjur.co.nr
RENJUCOOL
RENJUR

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 2 PinmemberLalit K Chaudhary4:31 13 May '10  
GeneralFile is wrong PinmemberMember 289604220:58 22 Dec '09  
GeneralMy vote of 1 Pinmemberbolivar1232:18 2 Jul '09  
GeneralMy vote of 1 PinmemberPrajnan Das1:29 17 Apr '09  
QuestionDude wheres my article? Pinmemberstensones1:55 14 Jun '07  
AnswerRe: Dude wheres my article? PinmemberRENJUR18:38 14 Jun '07  

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

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

Permalink | Advertise | Privacy | Mobile
Beta | 2.5.120515.1 | Last Updated 2 Jul 2009
Article Copyright 2007 by RENJUR
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid