question

UlrichEul-3418 avatar image
0 Votes"
UlrichEul-3418 asked UlrichEul-3418 answered

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

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).

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered

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);
             }
         }
     }
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

UlrichEul-3418 avatar image
0 Votes"
UlrichEul-3418 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

UlrichEul-3418 avatar image
0 Votes"
UlrichEul-3418 answered Castorix31 commented

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?

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The test is in case there is Outlook already opened, otherwise you must do :

 oOutlookApp = new Outlook.Application();

and release the instance at the end

  oOutlookApp.Quit();
  Marshal.ReleaseComObject(oOutlookApp);

And both your application and Outlook must be run in the same context
(Admin/Admin or Non-Admin/Non-Admin)





0 Votes 0 ·
UlrichEul-3418 avatar image
0 Votes"
UlrichEul-3418 answered Castorix31 commented

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)). "



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I cannot reproduce your error
It works fine for me with Outlook 2016 (32-bit on Windows 10 64-bit) as Non-Admin

Maybe you can search on Google with keywords like :

"TYPE_E_CANTLOADLIBRARY" interop

0 Votes 0 ·
UlrichEul-3418 avatar image
1 Vote"
UlrichEul-3418 answered

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 :).

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

UlrichEul-3418 avatar image
0 Votes"
UlrichEul-3418 answered Castorix31 edited

But my problem that I can't read receiver and sender in the Reading Pane in Outlook with VB.net or C# is not solved. In the appendix I am attaching a screenshot in which I present the problem again. In the picture you see the red arrows in the reading pane/area from outlook. The reading pane/area open when you click in the reading pane/area on the button Reply (I think that is the Name from the button in an English MS-Outlook). I want read the Adress from Sender (Von) and the recipient/receiver(An) in the reading pane/area. If you click the Reply button in the reading area, no new window will open. The answer mail is answered directly in the main Outlook window. I want know, what is the refrence from the reading pane/area in Visual Studio?

82005-screenshot-outlook.jpg




· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

In my version (2016), I have only those buttons (To and Cc) after I clicked "Reply" :

82045-outlookpane.jpg

where
To = mailItem.SenderEmailAddress
(as it is a reply, it is the address of the Sender)
I don't have From, but as it is your current email address,
From = mailItem.To





0 Votes 0 ·
outlookpane.jpg (33.8 KiB)

I did some tests in Debug, and this Pane seems to be : Explorer.ActiveInlineResponse
I get it by :

  var resp = oOutlookApp.ActiveExplorer().ActiveInlineResponse;



0 Votes 0 ·
UlrichEul-3418 avatar image
0 Votes"
UlrichEul-3418 answered Castorix31 commented

Hello Castorix31,

how must I change your Code form above. I try it with oOutlookApp.ActiveExplorer().ActiveInlineResponse; but I receive everytimes errors. Must I use the code var resp = oOutlookApp.ActiveExplorer().ActiveInlineResponse; in connection with oOutlookApp = Marshal.GetActiveObject("Outlook.Application") as Microsoft.Office.Interop.Outlook.Application;?
Sorry, I am not the specialist in c#.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yes, I tested with an opened Outlook instance .
It is not null only if Reply has been clicked :

 oOutlookApp = Marshal.GetActiveObject("Outlook.Application") as Microsoft.Office.Interop.Outlook.Application;
 if (oOutlookApp != null)
 {  
     var resp = oOutlookApp.ActiveExplorer().ActiveInlineResponse; 
     if (resp != null)
     {  
     // etc...



0 Votes 0 ·
UlrichEul-3418 avatar image
0 Votes"
UlrichEul-3418 answered

Hello Castorix31,

thank you very much for your help :), it's run fine. A last question please, how can I degug the code so that I can see that the refrence from the window in Outlook is in example Explorer.ActiveInlineResponse?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.