I am trying to create a VSTO outlook 2010 addin using vs2013 c#.
So far I have a ribbon bar with a button which are appearing on the outlook ribbon. When I click the button, it simply displays a message box.
How do I get the button to print the selected email?
Use the PrintOut method on the MailItem object to print out an email.
Explorer Window Code
If your ribbon button is displayed in the Outlook Explorer window you can print out all the selected emails using the following code:
Explorer explorer = Globals.ThisAddIn.Application.ActiveWindow() as Explorer;
Selection selection = explorer.Selection;
for (int i = 1; i <= selection.Count; i++)
{
var selectedItem = selection[i];
if (selectedItem is MailItem)
{
MailItem mailItem = selectedItem as MailItem;
mailItem.PrintOut();
}
Marshal.ReleaseComObject(selectedItem);
}
Marshal.ReleaseComObject(selection);
Inspector Window code
And if your button is displayed in the Inspector window you can print out the email using the following code:
Inspector inspector = Globals.ThisAddIn.Application.ActiveWindow() as Inspector;
var currentItem = inspector.CurrentItem;
if (currentItem is MailItem)
{
MailItem mailItem = currentItem as MailItem;
mailItem.PrintOut();
}
Marshal.ReleaseComObject(currentItem);
ReleaseComObject
Also notice I've used the ReleaseComObject method to release the COM objects. For more information about when to use the ReleaseComObject method see When ReleaseComObject is necessary in Outlook