none
Access a outlook mail item in a taskpane RRS feed

  • Question

  • I have made an add-in in which i built a task pane and in this addin class i have created a outlook.mailitem item which holds the current mail which has been selected in inbox.

     

    I want to access this "item" in task pane so that i can access properties of currently selected mail. I figured out this can be done by globals.thisaddin but was not able to do it.

     

    this is my code by which i am getting the currently selected mail.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using Microsoft.Office.Tools;
    
    namespace OutlookAddIn1
    {
      public partial class ThisAddIn
      {
        public Outlook.Explorer explorer;
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
          myUserControl1 = new MyUserControl1();
          myCustomTaskPane = this.CustomTaskPanes.Add(myUserControl1, "Xhance ");
          myCustomTaskPane.Visible = true;
          explorer = Application.Explorers[1];
          explorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(explorer_SelectionChange);
           
        }
    
        void explorer_SelectionChange()
        {
          for (int i = 1; i <= explorer.Selection.Count; i++)
          {
            Outlook.MailItem item = explorer.Selection[i] as Outlook.MailItem;
            if (item != null)
            {
              System.Windows.Forms.MessageBox.Show(item.SenderEmailAddress);
    
            }
          }
        }
        
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }
    
        #region VSTO generated code
    
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        
        private void InternalStartup()
        {
          this.Startup += new System.EventHandler(ThisAddIn_Startup);
          this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        private MyUserControl1 myUserControl1;
        private CustomTaskPane myCustomTaskPane;
        #endregion
        
    
      }
    
      
    }
    
    


    Now is there any way to access "item" in task pane.

     

    Thanks in advance.

    Friday, August 5, 2011 7:45 AM

Answers

  • Your task pane code should be able to access the Explorer.Selection collection and any item there.
     
        explorer.Selection[i] as Outlook.MailItem
     
    should work just as well from the task pane code.
     
    What's not working?

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "abhishekismad" <=?utf-8?B?YWJoaXNoZWtpc21hZA==?=> wrote in message news:958eeb51-651f-4365-853b-ad80f975c460...

    I have made an add-in in which i built a task pane and in this addin class i have created a outlook.mailitem item which holds the current mail which has been selected in inbox.

     

    I want to access this "item" in task pane so that i can access properties of currently selected mail. I figured out this can be done by globals.thisaddin but was not able to do it.

     

    this is my code by which i am getting the currently selected mail.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using Microsoft.Office.Tools;
    
    namespace OutlookAddIn1
    {
     public partial class ThisAddIn
     {
      public Outlook.Explorer explorer;
      private void ThisAddIn_Startup(object sender, System.EventArgs e)
      {
       myUserControl1 = new MyUserControl1();
       myCustomTaskPane = this.CustomTaskPanes.Add(myUserControl1, "Xhance ");
       myCustomTaskPane.Visible = true;
       explorer = Application.Explorers[1];
       explorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(explorer_SelectionChange);
        
      }
    
      void explorer_SelectionChange()
      {
       for (int i = 1; i <= explorer.Selection.Count; i++)
       {
        Outlook.MailItem item = explorer.Selection[i] as Outlook.MailItem;
        if (item != null)
        {
         System.Windows.Forms.MessageBox.Show(item.SenderEmailAddress);
    
        }
       }
      }
      
      private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
      {
      }
    
      #region VSTO generated code
    
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      
      private void InternalStartup()
      {
       this.Startup += new System.EventHandler(ThisAddIn_Startup);
       this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
      }
      private MyUserControl1 myUserControl1;
      private CustomTaskPane myCustomTaskPane;
      #endregion
      
    
     }
    
     
    }
    
    


    Now is there any way to access "item" in task pane.

     

    Thanks in advance.


    Ken Slovak MVP - Outlook
    • Marked as answer by abhishekismad Monday, August 8, 2011 8:01 AM
    Friday, August 5, 2011 1:23 PM

All replies

  • Your task pane code should be able to access the Explorer.Selection collection and any item there.
     
        explorer.Selection[i] as Outlook.MailItem
     
    should work just as well from the task pane code.
     
    What's not working?

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "abhishekismad" <=?utf-8?B?YWJoaXNoZWtpc21hZA==?=> wrote in message news:958eeb51-651f-4365-853b-ad80f975c460...

    I have made an add-in in which i built a task pane and in this addin class i have created a outlook.mailitem item which holds the current mail which has been selected in inbox.

     

    I want to access this "item" in task pane so that i can access properties of currently selected mail. I figured out this can be done by globals.thisaddin but was not able to do it.

     

    this is my code by which i am getting the currently selected mail.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using Microsoft.Office.Tools;
    
    namespace OutlookAddIn1
    {
     public partial class ThisAddIn
     {
      public Outlook.Explorer explorer;
      private void ThisAddIn_Startup(object sender, System.EventArgs e)
      {
       myUserControl1 = new MyUserControl1();
       myCustomTaskPane = this.CustomTaskPanes.Add(myUserControl1, "Xhance ");
       myCustomTaskPane.Visible = true;
       explorer = Application.Explorers[1];
       explorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(explorer_SelectionChange);
        
      }
    
      void explorer_SelectionChange()
      {
       for (int i = 1; i <= explorer.Selection.Count; i++)
       {
        Outlook.MailItem item = explorer.Selection[i] as Outlook.MailItem;
        if (item != null)
        {
         System.Windows.Forms.MessageBox.Show(item.SenderEmailAddress);
    
        }
       }
      }
      
      private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
      {
      }
    
      #region VSTO generated code
    
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      
      private void InternalStartup()
      {
       this.Startup += new System.EventHandler(ThisAddIn_Startup);
       this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
      }
      private MyUserControl1 myUserControl1;
      private CustomTaskPane myCustomTaskPane;
      #endregion
      
    
     }
    
     
    }
    
    


    Now is there any way to access "item" in task pane.

     

    Thanks in advance.


    Ken Slovak MVP - Outlook
    • Marked as answer by abhishekismad Monday, August 8, 2011 8:01 AM
    Friday, August 5, 2011 1:23 PM
  • It gives the error 'System.Windows.Forms.Application' does not contain a definition for 'Explorers' .

     


    Kumar Abhishek Always Ready to Learn:)
    Saturday, August 6, 2011 1:57 PM
  • Hey Ken

     

    Thanks for your help. I got it working now.

    This is what i did

    In usercontrol

     

    public partial class MyUserControl1 : UserControl
      {
        public Outlook.Explorer explorer;
         
        public MyUserControl1()
        {
          InitializeComponent();
          
        }
    
    private void MyUserControl1_Load(object sender, EventArgs e)
        {
          explorer = Globals.ThisAddIn.Application.Explorers[1];
          explorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(explorer_SelectionChange);
          
        }
    
        void explorer_SelectionChange()
        {
          
          for (int i = 1; i <= explorer.Selection.Count; i++)
          {
            Outlook.MailItem item = explorer.Selection[i] as Outlook.MailItem;
    
            if (item != null)
            {
                        label1.Text=(item.SenderName);
              
            }
          }
        }<br/><br/>Thanks a lot ! <br/>
    



    Kumar Abhishek Always Ready to Learn:)
    Monday, August 8, 2011 8:01 AM