Massoud Mazar's Blog

Sharing The Knowledge

About the author

Massoud Mazar has been doing .net development since 2002. He is founder and president of Evolynx, Inc.
E-mail me Send mail

Recent comments

Don't show

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Outlook add-in to select "From Account"

If you have setup more than one email account in your Outlook (Work and Home) you may have realized a need for some type of pop-up dialog which will ask you which account you want to use when creating a new email. Outlook lets you define a default account, but obviously that's not good enough. I decided to write a small and simple add-in which will ask me to select the sending account every time I compose a new email. (Even if Outlook already has such a tool, I couldn't find it! and I wanted to gain some Office programming knowledge anyways.)

I used Visual Studio 2008 and created a new Office project of type Outlook Add-in. This will give you the basics you need. Just fill the methods with your code and you are done.

    public partial class ThisAddIn
    {
        private Outlook.Inspectors Inspectors;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Inspectors = this.Application.Inspectors;
            Inspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);

            foreach (Outlook.Inspector inspector in Inspectors)
            {
                Inspectors_NewInspector(inspector);
            }
        }

        void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
        {
            if (Inspector.CurrentItem is Outlook.MailItem)
            {
                Outlook.MailItem item = (Outlook.MailItem)Inspector.CurrentItem;
                if (string.IsNullOrEmpty(item.Body) && string.IsNullOrEmpty(item.Subject) && string.IsNullOrEmpty(item.To))
                {
                    Outlook.MailItem ThisItem = (Outlook.MailItem)Inspector.CurrentItem;
                    Accounts accForm = new Accounts();
                    System.Windows.Forms.ComboBox cboAccounts = (System.Windows.Forms.ComboBox)accForm.Controls["cboAccounts"];
                    foreach (Outlook.Account account in this.Application.Session.Accounts)
                    {
                        cboAccounts.Items.Add(account.DisplayName);
                    }
                    cboAccounts.SelectedIndex = 0;
                    accForm.ShowDialog();
                    ThisItem.SendUsingAccount = this.Application.Session.Accounts[cboAccounts.SelectedIndex + 1];
                }
            }
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            Inspectors.NewInspector -= new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
            Inspectors = null;
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
       
        #endregion
    }

 

Accounts() is a Windows Form which only contains a dropdown and a button:

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Outlook add-in
Posted by Mazar on Monday, June 16, 2008 8:56 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Friday, November 21, 2008 11:35 AM