Home > database >  Outlook Add In VSTO
Outlook Add In VSTO

Time:11-18

I'm still quite in the field of programming. But now I have come across small problem. I'm currently working on an Outlook add-in that takes an email and packs it into a new mail as an attachment and automatically sends it to a specific address. That works well so far. However, it only works if I have opened the mail that I want to send as an attachment in a new window.

My goal, however, would be that it is already sufficient if the mail is open in the reading area. However, I have unfortunately not found a way to address this area. Maybe someone can give me a link or an example to address the mail, which is currently displayed in the preview with Visual Studio. As a language I use C #.

So far I reach the opened mail as follows:

String path = "C:\\Test\\Mail.msg";

Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
MailItem mailitem = inspector.CurrentItem as MailItem

But how do I reach the mail that can be seen as a preview in the reading area?

Thank you very much for your help.

CodePudding user response:

Use Application.ActiveExplorer.Selection collection to loop through the selected messages and process them appropriately.

Keep in mind that (just like with Application.ActiveInspector), you can have items other than MailItem, e.g. ContactItem, AppointmentItem, ReportItem, etc. You need to handle items like that.

CodePudding user response:

To get the currently selected item from the Explorer window in Outlook you need to use the following code:

        if (Application.ActiveExplorer().Selection.Count > 0)
        {
            Object selObject = this.Application.ActiveExplorer().Selection[1];
            if (selObject is Outlook.MailItem)
            {
                Outlook.MailItem mailItem =
                    (selObject as Outlook.MailItem);
                itemMessage = "The item is an e-mail message."  
                    " The subject is "   mailItem.Subject   ".";
                mailItem.Display(false);
            }
            else if (selObject is Outlook.ContactItem)
            {
                Outlook.ContactItem contactItem =
                    (selObject as Outlook.ContactItem);
                itemMessage = "The item is a contact."  
                    " The full name is "   contactItem.Subject   ".";
                contactItem.Display(false);
            }
            else if (selObject is Outlook.AppointmentItem)
            {
                Outlook.AppointmentItem apptItem =
                    (selObject as Outlook.AppointmentItem);
                itemMessage = "The item is an appointment."  
                    " The subject is "   apptItem.Subject   ".";
            }
            else if (selObject is Outlook.TaskItem)
            {
                Outlook.TaskItem taskItem =
                    (selObject as Outlook.TaskItem);
                itemMessage = "The item is a task. The body is "
                      taskItem.Body   ".";
            }
            else if (selObject is Outlook.MeetingItem)
            {
                Outlook.MeetingItem meetingItem =
                    (selObject as Outlook.MeetingItem);
                itemMessage = "The item is a meeting item. "  
                     "The subject is "   meetingItem.Subject   ".";
            }
        }

But if you need to get all selected items in the Explorer view you need to iterate over all items in the Selection object.

  • Related