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:
- Opens a form which can be filled with a message
- Edits an image and writes the message within it
- Sends an e-mail with the image attached
Using the Code
First, create a new project with the template Office – Outlook 2010 Add-in.
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
.
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.
Add an image to the button if desired and write the label on it.
Add a new form to the project.
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:
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;
private void ShowForm()
{
if (form1 == null)
{
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.
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:
PointF location = new PointF(137f, 150f);
string imageFilePath = Path.Combine
(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "picture.png");
Bitmap bitmap = (Bitmap)Image.FromFile(imageFilePath);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
using (Font arialFont = new Font("Comic Sans", 2))
{
if (!string.IsNullOrWhiteSpace(to))
{
graphics.DrawString(to + ",", arialFont, Brushes.CornflowerBlue, location);
}
}
}
imageFilePath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
"picture1.png") bitmap.Save(imageFilePath);
Then send the image:
private void SendMessage(Microsoft.Office.Interop.Outlook.Application oApp,
List<string> bodyMessage, string stringBodyMessage,
string receiver, string subject, string from, string to )
{
try
{
Microsoft.Office.Interop.Outlook.MailItem oMsg =
(Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem
(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
oMsg.HTMLBody = stringBodyMessage;
String sDisplayName = "MyAttachment";
int iPosition = (int)oMsg.Body.Length + 1;
int iAttachType = (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue;
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);
oMsg.Subject = subject;
Microsoft.Office.Interop.Outlook.Recipients oRecips =
(Microsoft.Office.Interop.Outlook.Recipients)oMsg.Recipients;
Microsoft.Office.Interop.Outlook.Recipient oRecip =
(Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add(receiver);
oRecip.Resolve();
oMsg.Send();
oRecip = null;
oRecips = null;
oMsg = null;
oApp = null;
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show("Ocorreu um erro " + ex.Message);
}
}
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.
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.
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.
Drag the Software – Microsoft – Office – Outlook – Addins – Name of the Add in from the top view to the bottom view under HKEY_CURRENT_USER
.
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