How can I read out the receiver and sender in the Reading Pane in Outlook with VB.net or C#

Ulrich Eul 26 Reputation points
2021-03-24T08:35:08.487+00:00

How can I read out the receiver and sender in the Reading Pane in Outlook with VB.net or C#. It goes in a new window in which a new mail is created with

objOL = CreateObject ("Outlook.Application")
MsgBox ("objOL.ActiveInspector.CurrentItem.To:" & objOL.ActiveInspector.CurrentItem.To

It doesn't work in the reading pane (docked window).

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
6,977 questions
No comments
{count} votes

Accepted answer
  1. Castorix31 65,711 Reputation points
    2021-03-24T17:40:15.247+00:00

    Thsi test in c# works for me on an opened Outlook instance (Outlook 2016, Windows 10) =>

    Microsoft.Office.Interop.Outlook.Application oOutlookApp= null;
    if (Process.GetProcessesByName("outlook").Count() > 0)
    {
        oOutlookApp = Marshal.GetActiveObject("Outlook.Application") as Microsoft.Office.Interop.Outlook.Application;
        if (oOutlookApp != null)
        {
            Microsoft.Office.Interop.Outlook.MAPIFolder selectedFolder = oOutlookApp.ActiveExplorer().CurrentFolder;
            if (oOutlookApp.ActiveExplorer().Selection.Count > 0)
            {
                Object selObject = oOutlookApp.ActiveExplorer().Selection[1];
                if (selObject is Microsoft.Office.Interop.Outlook.MailItem)
                {
                    Microsoft.Office.Interop.Outlook.MailItem mailItem = (selObject as Microsoft.Office.Interop.Outlook.MailItem);
                    string sSender = mailItem.SenderName;
                    string sTo = mailItem.To;
                    System.Windows.Forms.MessageBox.Show("Sender : " + sSender + Environment.NewLine + "To : "  + sTo + Environment.NewLine, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
    
    No comments

7 additional answers

Sort by: Most helpful
  1. Ulrich Eul 26 Reputation points
    2021-03-26T15:09:14.13+00:00

    Hello Castorix31,

    thank you very much for your tip with search for "TYPE_E_CANTLOADLIBRARY" interop. I found, that the reason can be, that the MS-Office is corrupt, it was true, I repair my MS-Office an now it's run :).

    No comments

  2. Ulrich Eul 26 Reputation points
    2021-03-25T07:29:45.927+00:00

    Thank you very much, I test your example in the next time.

    No comments

  3. Ulrich Eul 26 Reputation points
    2021-03-26T12:28:01.85+00:00

    I test the code in VS 2017 and 2019 both in a Windows Form bases application, but it's occur an error.

    System.Runtime.InteropServices.COMException: Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE)).

    What can I do?


  4. Ulrich Eul 26 Reputation points
    2021-03-26T13:59:33.083+00:00

    Hello Castorix31,

    I have simplified the code, as soon as a folder (row 10), for example, is accessed, the error message appears. Outlook is open and all in the same context (Admin/Admin or Non-Admin/Non-Admin)

    private void Button2_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Outlook.Application oOutlookApp = null;
            System.Windows.Forms.MessageBox.Show("Count : " + Process.GetProcessesByName("outlook").Count());
            if (Process.GetProcessesByName("outlook").Count() > 0)
            {
                oOutlookApp = Marshal.GetActiveObject("Outlook.Application") as Microsoft.Office.Interop.Outlook.Application;
                if (oOutlookApp != null)
                {
                    Microsoft.Office.Interop.Outlook.MAPIFolder selectedFolder = oOutlookApp.ActiveExplorer().CurrentFolder;
    
                    oOutlookApp.Quit();
                    Marshal.ReleaseComObject(oOutlookApp);
                }
            }
        }
    

    Error message (translate from German to English) :
    System.InvalidCastException: "The COM object of the type" Microsoft.Office.Interop.Outlook.ApplicationClass "cannot be converted to the interface type" Microsoft.Office.Interop.Outlook._Application ". This operation could not be performed because the QueryInterface call to the COM component for the interface with the IID "{00063001-0000-0000-C000-000000000046}" could not be carried out due to the following error: Error loading the type library / DLL (exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)). "