Click here to Skip to main content
15,886,689 members
Articles / Web Development / HTML
Alternative
Article

Creating an Outlook 2010 Add-In with a Custom Send Button

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
22 Dec 2014CPOL3 min read 44.6K   1.4K   9   5
This is an alternative for "Creating an Outlook 2010 Add-In"

Introduction

This tip will cover how to create a button on a ribbon and send a custom message on a postal. It will also cover the installation. It was developed using C# in VS2012 and Office 2010.

Background

I was asked to somehow create conditions for non-IT technicians to edit a Christmas postal, allowing them to send a custom message to the people they cared.

I created a button on a ribbon that:

  1. Opens a form which can be filled with a message
  2. Edits an image and writes the message within it
  3. Sends an e-mail with the image attached

Using the Code

First, create a new project with the template Office – Outlook 2010 Add-in.

Image 1

Then add a ribbon (Add New Item – Ribbon (Visual Designer)). The ribbon comes with a built in Tab and a Group which organizes controls on the ribbon. Change the labels of the tab and the group. Change the ControlIdType property of the tab to Custom and change the RibbonType of the Ribbon to Microsoft.Outlook.Explorer.

Image 2

At this point, you can click on Start and it will open Outlook with the Ribbon. Using the Toolbox, Add an office Ribbon Control Button to the Group.

Image 3

Add an image to the button if desired and write the label on it.

Add a new form to the project.

Image 4

On the button below, add a code on which after clicking it, it will display the form. This is the code for the Ribbon.cs:

C#
public partial class MyOutlookAddIn
{
    private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
    {

    }

    private void button1_Click(object sender, RibbonControlEventArgs e)
    {
        ShowForm();
    }

    private Form1 form1 = null;
    //this is to show the form    
    private void ShowForm()
    {
        if (form1 == null)
        {                //uses the Global class variable to fetch the Form
            form1 = new Form1(Globals.ThisAddIn.Application);
        }
        form1.ShowDialog();
    }
}

Draw a form that will capture all the necessary information (subject, message content, e-mail, etc.). Add an image to the project, which will be the background of the postal. On the properties, put copy to Output Directory – Copy if newer.

Make sure you change the constructor of the form to receive an Outlook Application on the constructor.

C#
public partial class Form1 : Form
    {
        protected Microsoft.Office.Interop.Outlook.Application App;

        public Form1(Microsoft.Office.Interop.Outlook.Application _app)
        {
            App = _app;
            InitializeComponent();
        }

On click of the button send, it will write the content of the message on specific locations of the image using the code below:

C#
//Create the location point
PointF location = new PointF(137f, 150f);           

//get the message located on the installation folder of the add-in
string imageFilePath = Path.Combine
(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "picture.png");

//Convert the image to a manageable form (Bitmap)                           
Bitmap bitmap = (Bitmap)Image.FromFile(imageFilePath);

using (Graphics graphics = Graphics.FromImage(bitmap))
{
   using (Font arialFont = new Font("Comic Sans", 2))
   {       
       if (!string.IsNullOrWhiteSpace(to))
       {     //draw on it
            graphics.DrawString(to + ",", arialFont, Brushes.CornflowerBlue, location);
       }
   }
}

   //it then saves the image using another name, avoiding it to be overriden.
  imageFilePath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, 
  "picture1.png")   bitmap.Save(imageFilePath);

Then send the image:

C#
private void SendMessage(Microsoft.Office.Interop.Outlook.Application oApp, 
List<string> bodyMessage, string stringBodyMessage, 
string receiver, string subject, string from, string to )
        {
           try
           {                              
               // Create a new mail item. Pass the application received on the form as parameter
               Microsoft.Office.Interop.Outlook.MailItem oMsg = 
               (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem
               (Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

                //add the body of the email
                oMsg.HTMLBody = stringBodyMessage; 
                //Add an attachment.
                String sDisplayName = "MyAttachment";
                int iPosition = (int)oMsg.Body.Length + 1;
                int iAttachType = (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue;
                //now attached the file
                prepareMessage(bodyMessage, from, to);
                clearFieldsAndClose();

                string thePath = Path.Combine
                (AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "picture1.png");
                Microsoft.Office.Interop.Outlook.Attachment oAttach = 
                    oMsg.Attachments.Add(thePath, iAttachType, iPosition, sDisplayName);
                
                //Subject line
                oMsg.Subject = subject;
                // Add a recipient.
                Microsoft.Office.Interop.Outlook.Recipients oRecips = 
                    (Microsoft.Office.Interop.Outlook.Recipients)oMsg.Recipients;
                // Change the recipient in the next line if necessary.
                Microsoft.Office.Interop.Outlook.Recipient oRecip = 
                    (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add(receiver);
                oRecip.Resolve();
                // Send.
                oMsg.Send();
                // Clean up.
                oRecip = null;
                oRecips = null;
                oMsg = null;
                oApp = null;
                
            }//end of try block
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Ocorreu um erro " + ex.Message);
            }//end of catch
        }

Installing the Add-In

Click on File => Add => New Project => Other Project Types => Setup and Deployment => Install shield. If you do not have installshield, you can download it from here.

Image 5

Use the project assistant and fill the Application information, Installation Requirements. Take special care on Application Files. Click the Add Project Outputs and Add the primary output. Then add also the file manifest and the file vsto, all located on the bin folder created after building. Add the image also.

Image 6

The last step of installation is to add a Registry on Application Registry. Initially, it might be blanked under HKEY_CURRENT_USER, but if you click on Registry on the left, you will be redirected to a view where you can drag and drop.

Image 7

Drag the Software – Microsoft – Office – Outlook – Addins – Name of the Add in from the top view to the bottom view under HKEY_CURRENT_USER.

Image 8

This view initially will be displayed with the path of the development. You will have to edit the Manifest entry to [INSTALLDIR], so that it will run using the installation path. (View previous image.)

Points of Interest

For further reading, please refer to:

Some troubleshooting tips:

History

  • 22nd December, 2014 - Initial post

License

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


Written By
Software Developer Banco de Moçambique
Mozambique Mozambique
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGood Job Turay Pin
davton23-Dec-14 6:39
davton23-Dec-14 6:39 
AnswerRe: Good Job Turay Pin
Turay Melo23-Dec-14 19:55
professionalTuray Melo23-Dec-14 19:55 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun22-Dec-14 19:58
Humayun Kabir Mamun22-Dec-14 19:58 
GeneralRe: My vote of 5 Pin
Anurag Gandhi22-Dec-14 22:14
professionalAnurag Gandhi22-Dec-14 22:14 
GeneralRe: My vote of 5 Pin
Turay Melo23-Dec-14 0:35
professionalTuray Melo23-Dec-14 0:35 

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.