Month: July 2014

Apply custom icons to Ribbon Menu Controls

In this post we will be focusing on how to apply custom icons of your choice to the controls that you have created in the Ribbon Menu of Outlook.

You can use the imageMso property of the controls and put any of the default provided icons.Microsoft by default provide a vast set of icons for applying on the controls.You can find this list here.

But some times because of client requirements or any other reason we require to place our own custom icon like the company logo or anything. In this post we will be discussing on how to achieve it.

Addin

You have to save the custom icons that you want to show on the controls in your application resources then provide the loadImage attribute to the CustomUI of your ribbon file and then image tag in your individual controls.

<?xml version=”1.0″ encoding=”UTF-8″?>
<customUI xmlns=”http://schemas.microsoft.com/office/2009/07/customui&#8221; onLoad=”Ribbon_Load” loadImage=”GetImage” >
<ribbon>
<tabs>
<tab idMso=”TabMail”>
<group id=”MyPreferenceGroup”
label=”MyMenu”>
<button id=”btnPreferences” size=”large”  onAction=”btnPreferences_click” image=”Preference”/>
</group>
</tab>
<tab idMso=”TabNewMailMessage”>
<group id=”MyAttachGroup” label=”YOYO”>
<button id=”btnAttach” size=”large” onAction=”btnAttach_click” image=”Attach”/>
<button id=”btnSetting” size=”large” onAction=”btnSettings_click” image=”Settings”/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>

Now you can see in the above code we have three buttons having image tags and in the custom UI tag we have loadImage=”GetImage” attribute.

Now on the server side of the ribbon we will implement the GetImage method.

public stdole.IPictureDisp GetImage(string strImageName)
{
try
{
switch (strImageName)
{
case Preference:
return PictureConverter.IconToPictureDisp(Properties.Resources.preferences_settings);
case Attach:
return PictureConverter.IconToPictureDisp(Properties.Resources.Attach);
case Settings:
return PictureConverter.IconToPictureDisp(Properties.Resources.Settings);
default:
return PictureConverter.IconToPictureDisp(Properties.Resources.preferences_settings);
}
}
catch (Exception ex)
{
LogWriter.LogErrorMessage(ex);
return PictureConverter.IconToPictureDisp(Properties.Resources.preferences_settings);
}
}

In the above method you can see based on the input means from which button(out of three) this call is made we are returning the corresponding image.

Now basically the icons stored in application resources will be of .ico extension but we need to convert them to the DISP, for this purpose we create a picture converter as shown below.

internal class PictureConverter : AxHost
{
private PictureConverter() : base(String.Empty) { }

static public stdole.IPictureDisp ImageToPictureDisp(Image image)
{
return (stdole.IPictureDisp)GetIPictureDispFromPicture(image);
}

static public stdole.IPictureDisp IconToPictureDisp(Icon icon)
{
return ImageToPictureDisp(icon.ToBitmap());
}

static public Image PictureDispToImage(stdole.IPictureDisp picture)
{
return GetPictureFromIPicture(picture);
}
}

So this is how one can apply icons of his choice on the Ribbon menu controls. If there are more controls then the cases in the switch statement will increase accordingly.

Hope this helps.

 

Advertisements

Initiate action when a new mailitem arrive in an Outlook folder

Some times we need to trigger an action when a new mailitem arrives in a Outlook Folder.

Requisite may be anything from notifying the user about the new Mailitem to manipulate the arriving mailitem.

We can achieve this functionality by registering the YourFolder_ItemAddevent of the required folder in the ThisAddIn_Startup event and then performing the required action in the YourFolder_ItemAdd event.

 

Following is the ThisAddIn_Startup method :

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{

string folderPath =

Application.Session.
DefaultStore.GetRootFolder().FolderPath
+ @”\Outbox\My Outbox”;
Outlook.Folder quatFolder = GetFolder(folderPath);

_OutboxItems = quatFolder.Items;
_OutboxItems.ItemAdd += OutboxItems_ItemAdd;
Marshal.ReleaseComObject(quatFolder);

}

 

In the above method we have defined an event that will run whenever a item arrives in the My Outbox folder of Outlook.

private void OutboxItems_ItemAdd(object item)
{
if (item is MailItem)
{
Outlook.MailItem mailItem = item as Outlook.MailItem;

if (mailItem != null)
{

//Here you can do whatever is the required action that you want to perform

}

}

 

In the above method first we have checked that the incoming item is a mailIItem and then we can perform desired action.

We can manipulate the properties of the mailItem like attachments , To , CC , BCC and others.

We can notify the user about the same by showing a messagebox.

This is how we can trigger a code on the incoming mailItem on a Outlook Folder.

Adding a new folder in Outlook through C#

I guess most of us are familiar with Outlook addin’s . Add Ins make our work easier by providing some Out Of Box Features .

In this post we will be looking into how to create a new Folder in the outlook as and when our Plugin starts.

Video

Open a Outlook addin project in your Visual Studio and open the ThisAddIn class.

ThisAddIn_StartUp method will be called every time you starts the plugin or addin.

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
AddMyNewFolder();

}

Inside startup method call a folder creation method.

private void AddMyNewFolder()
{

Outlook.Folder folder =Application.Session.GetDefaultFolder(
Outlook.OlDefaultFolders.olFolderOutbox) as Outlook.Folder;
Outlook.Folders folders = folder.Folders;
try
{
if (folders.Count == 0)
{
Outlook.Folder newFolder = folders.Add(“Your Folder Name”, Type.Missing) as Outlook.Folder;
newFolder.Display();
}
}
catch (Exception ex)
{
MessageBox.Show(
“Could not add ‘Your Folder Name'”,”Add Folder”,
MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}

In the above method you will be seeing that first we have created a parent folder object , in this case the Outbox , and then creating a child folder.

You can set the parent folder by choosing a option from Outlook.OlDefaultFolders .

If instead of a sub folder you want to create a parent folder then in that case manipulate

Outlook.Folders folders = folder.Folders;

As

Outlook.Folders folders = folder.Parent.Folders;

Plenty of custom functionality can be achieved through addins. Will be coming up with some others.