Reputation: 1082
Here I'm going to create an Outlook add in which extracts an attachment from Outlook using C#.
I place a button on Outlook using add ins and on on_click
event, I called this method below; code is working fine. It's extracting all attachments which are placed in inbox of Outlook, but I want only attachment which I select from mouse.
Can any one help me to out this issue?
private void ThisApplication_NewMail()
{
Outlook.MAPIFolder inBox = this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Items inBoxItems = inBox.Items;
Outlook.MailItem newEmail = null;
//inBoxItems = inBoxItems.Restrict("[Unread] = true");
try
{
foreach (object collectionItem in inBoxItems)
{
newEmail = collectionItem as Outlook.MailItem;
if (newEmail != null)
{
if (newEmail.Attachments.Count > 0)
{
for (int i = 1; i <= newEmail.Attachments.Count; i++)
{
newEmail.Attachments[i].SaveAsFile(@"C:\TestFileSave\" +
newEmail.Attachments[i].FileName);
}
}
}
}
}
catch (Exception ex)
{
string errorInfo = (string)ex.Message.Substring(0, 11);
if (errorInfo == "Cannot save")
{
System.Windows .Forms . MessageBox.Show(@"Create Folder C:\TestFileSave");
}
}
}
Upvotes: 1
Views: 6885
Reputation: 11063
Create a FormRegion control and insert it into your outlook message window.
Then when you click a message on your inbox you can get the message class with:
private void FormRegionMessageClassArchivadoFactory_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
{
Outlook.MailItem item = (Outlook.MailItem)e.OutlookItem;
if (item.Attachments.Count > 0)
{
int attachRestantes = item.Attachments.Count;
for (int j = attachRestantes; j >=1; j--)
{
//get attachments
}
}
}
EDIT:
To get the attachment content as byte use the following code.
//microsoft schema to get the attachment content
private string AttachSchema="http://schemas.microsoft.com/mapi/proptag/0x37010102";
Outlook.PropertyAccessor pacc = item.Attachments[j].PropertyAccessor;
byte[] filebyte = (byte[])pacc.GetProperty(AttachSchema);
Upvotes: 2
Reputation: 171
Is your issue about finding the selected email, or selecting an attachment in the current email ?
We extract the selected emails in the mailbox, then prompt the user to select the attachments to save in a custom form. Here is our routine to extract the email user selection.
Outlook.Explorer curExplorer = OutlookApplication.ActiveExplorer();
Outlook.NameSpace curNameSpace = OutlookApplication.GetNamespace("MAPI");
Outlook.MAPIFolder curFolder = curExplorer.CurrentFolder;
if (curExplorer.Selection != null && curExplorer.Selection.Count > 0)
{
// get mails
_lstMailItems = new List<Outlook.MailItem>();
try
{
foreach (Outlook.MailItem curMailItem in curExplorer.Selection)
{
// modification on mail items in plugin are repercuted in Outlook
_lstMailItems.Add(curMailItem);
}
}
catch (COMException exc)
{
// log, selected item is not an email.
}
}
Upvotes: 0
Reputation: 1700
I would look at hooking into the Explorer.Selection change event: http://msdn.microsoft.com/en-us/library/office/ff869813.aspx
Upvotes: 0