none
Outlook Interop VS2012 Outlook 2013 RRS feed

  • Question


  • using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Outlook = Microsoft.Office.Interop.Outlook; namespace MailTestAddIn { public partial class frmMailTest : Form { public frmMailTest() { InitializeComponent(); } private void MailTest_Load(object sender, EventArgs e) { } private void btnTest_Click(object sender, EventArgs e) { Outlook.Application myApp = new Outlook.Application(); Outlook.NameSpace appNameSpace = myApp.GetNamespace("MAPI"); Outlook.MAPIFolder inboxFolder = appNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); for (int i = 1; i <= inboxFolder.Items.Count; i++) { try { Outlook.MailItem item = (Outlook.MailItem)inboxFolder.Items[i]; MessageBox.Show(item.Subject.ToString()); } catch (System.Runtime.InteropServices.COMException ex) { MessageBox.Show(ex.ToString()); } } } } }

    Hi,

    As you can see by the code posting above, I am trying to get a from that launches with Outlook, and when you click the button it has to loop through all the emails, once that is done, it should basically just create a small list of emails that have not been responded to in the past 72 hours. Now in theory it should be quite simple, but I am having trouble with Outlook Interop services, it seems that Folder is not yet where it should go but in fact MAPIFolder, the trouble I have is that I cannot seem to access the emails within that folder.

    I have gone through a few forums and tutorials but for the life of me I cannot find what I am missing, please do keep in mind that I am still quite new to the entire interop services.

    Thanks.

    Hein

    • Moved by Kristin Xie Thursday, October 30, 2014 6:58 AM
    Wednesday, October 29, 2014 1:50 PM

Answers

  • To avoid this problem you need to check the Item type before casting. I have adjusted your code as follow:

                Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.Application();
                Microsoft.Office.Interop.Outlook.NameSpace appNameSpace = myApp.GetNamespace("MAPI");
    
                Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = appNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
    
                for (int i = 1; i <= inboxFolder.Items.Count; i++)
                {
                    try
                    {
                        if (inboxFolder.Items[i] is Microsoft.Office.Interop.Outlook.MailItem)
                        {
    
                            Microsoft.Office.Interop.Outlook.MailItem item = (Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items[i];
    
                            MessageBox.Show(item.Subject);
    
                        }
                    }
                    catch (System.Runtime.InteropServices.COMException ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myApp);

    Please, marks as answer if helpful.


    • Edited by Deric Ferreira Thursday, October 30, 2014 11:22 AM
    • Marked as answer by Heinz09 Thursday, October 30, 2014 11:31 AM
    Thursday, October 30, 2014 11:22 AM

All replies

  • Please, what is the exception or error are you getting? Does your outlook requiring authentication after opened?

    I have reproduced your code and it runs perfect in my Environment, using VS 2012 and Outlook 2013!


    Wednesday, October 29, 2014 2:05 PM
  • Yes it requires that I log in, I log in, when I have logged in then I click the test button.

    At the moment I just want it to loop through the emails in my Inbox, and bring up a messagebox with the all the emails subject in them. From there I want to go through the content and just save it to a database, though that part is simpler, at the moment my main gripe is with the interop that I am not experienced in using.

    Does it bring all the subjects up in a messagebox on your system?

    Thursday, October 30, 2014 8:05 AM
  • Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).


    ************** Exception Text **************
    System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
       at CallSite.Target(Closure , CallSite , Object )
       at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
       at MailTestAddIn.frmMailTest.btnTest_Click(Object sender, EventArgs e) in c:\Users\Heinz\Documents\Visual Studio 2012\Projects\MailTestAddIn\MailTestAddIn\MailTest.cs:line 37
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


    ************** Loaded Assemblies **************
    mscorlib
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.18444 built by: FX451RTMGDR
        CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
    ----------------------------------------
    Microsoft.VisualStudio.Tools.Office.Runtime
        Assembly Version: 10.0.0.0
        Win32 Version: 10.0.50701.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Office.Runtime/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Office.Runtime.dll
    ----------------------------------------
    System.Core
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
    ----------------------------------------
    System
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
    ----------------------------------------
    System.Configuration
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
    ----------------------------------------
    System.Xml
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.34234 built by: FX452RTMGDR
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
    ----------------------------------------
    System.Security
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Security/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Security.dll
    ----------------------------------------
    Microsoft.VisualStudio.Tools.Applications.Hosting
        Assembly Version: 10.0.0.0
        Win32 Version: 10.0.50701.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.Hosting/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.Hosting.dll
    ----------------------------------------
    Microsoft.VisualStudio.Tools.Applications.Runtime
        Assembly Version: 10.0.0.0
        Win32 Version: 10.0.50701.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.Runtime/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.Runtime.dll
    ----------------------------------------
    System.Deployment
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Deployment/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Deployment.dll
    ----------------------------------------
    Microsoft.VisualStudio.Tools.Applications.ServerDocument
        Assembly Version: 10.0.0.0
        Win32 Version: 10.0.50701.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.ServerDocument/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll
    ----------------------------------------
    System.Windows.Forms
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
    ----------------------------------------
    System.Drawing
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
    ----------------------------------------
    System.Xml.Linq
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml.Linq/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.Linq.dll
    ----------------------------------------
    Microsoft.Office.Tools
        Assembly Version: 10.0.0.0
        Win32 Version: 10.0.50701.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.dll
    ----------------------------------------
    Microsoft.Office.Tools.Outlook.Implementation
        Assembly Version: 10.0.0.0
        Win32 Version: 10.0.50701.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.Outlook.Implementation/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Outlook.Implementation.dll
    ----------------------------------------
    Microsoft.Office.Tools.Common.Implementation
        Assembly Version: 10.0.0.0
        Win32 Version: 10.0.50701.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.Common.Implementation/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Common.Implementation.dll
    ----------------------------------------
    Microsoft.Office.Tools.Common
        Assembly Version: 10.0.0.0
        Win32 Version: 10.0.50701.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.Common/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Common.dll
    ----------------------------------------
    Microsoft.Office.Tools.Outlook
        Assembly Version: 10.0.0.0
        Win32 Version: 10.0.50701.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.Outlook/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Outlook.dll
    ----------------------------------------
    MailTestAddIn
        Assembly Version: 1.0.0.0
        Win32 Version: 1.0.0.0
        CodeBase: file:///C:/Users/Heinz/Documents/Visual%20Studio%202012/Projects/MailTestAddIn/MailTestAddIn/bin/Debug/MailTestAddIn.DLL
    ----------------------------------------
    Microsoft.Office.Tools.Outlook.v4.0.Utilities
        Assembly Version: 10.0.0.0
        Win32 Version: 10.0.30319.1
        CodeBase: file:///C:/Users/Heinz/Documents/Visual%20Studio%202012/Projects/MailTestAddIn/MailTestAddIn/bin/Debug/Microsoft.Office.Tools.Outlook.v4.0.Utilities.DLL
    ----------------------------------------
    Microsoft.Office.Tools.Common.v4.0.Utilities
        Assembly Version: 10.0.0.0
        Win32 Version: 10.0.30319.1
        CodeBase: file:///C:/Users/Heinz/Documents/Visual%20Studio%202012/Projects/MailTestAddIn/MailTestAddIn/bin/Debug/Microsoft.Office.Tools.Common.v4.0.Utilities.DLL
    ----------------------------------------
    Microsoft.Office.Interop.Outlook
        Assembly Version: 15.0.0.0
        Win32 Version: 15.0.4569.1506
        CodeBase: file:///C:/Windows/assembly/GAC_MSIL/Microsoft.Office.Interop.Outlook/15.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.Outlook.dll
    ----------------------------------------
    office
        Assembly Version: 15.0.0.0
        Win32 Version: 15.0.4649.1000
        CodeBase: file:///C:/Windows/assembly/GAC_MSIL/office/15.0.0.0__71e9bce111e9429c/office.dll
    ----------------------------------------
    Microsoft.CSharp
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.18408
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.CSharp/v4.0_4.0.0.0__b03f5f7f11d50a3a/Microsoft.CSharp.dll
    ----------------------------------------
    System.Dynamic
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.18408
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Dynamic/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Dynamic.dll
    ----------------------------------------
    Anonymously Hosted DynamicMethods Assembly
        Assembly Version: 0.0.0.0
        Win32 Version: 4.0.30319.18444 built by: FX451RTMGDR
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/mscorlib/v4.0_4.0.0.0__b77a5c561934e089/mscorlib.dll
    ----------------------------------------
    Thursday, October 30, 2014 8:08 AM
  • Hello Hein,

    Did you try to debug the code? What property or method call exactly fires the exception?

    Now let's review your code:

    Outlook.MailItem item = (Outlook.MailItem)inboxFolder.Items[i];
    

    The Items collection may contain all type of Outlook items, not only mail items. I'd recommend checking a particular item first - whether it is a mail item or not.

    Also I'd recommend breaking the chain of calls declaring each property or method call on a separate line. Thus, you will be able to release all underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. For example, the Items property returns an instance of the corresponding class which should be released after. Then the indexer is used to get an Outlook item which is left unreleased also.

    The same can be applied to the following line of code:

    inboxFolder.Items.Count

    Each time the for loop iterates the reference counter is increased for the Items class.

    Thursday, October 30, 2014 8:09 AM
  • To avoid this problem you need to check the Item type before casting. I have adjusted your code as follow:

                Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.Application();
                Microsoft.Office.Interop.Outlook.NameSpace appNameSpace = myApp.GetNamespace("MAPI");
    
                Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = appNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
    
                for (int i = 1; i <= inboxFolder.Items.Count; i++)
                {
                    try
                    {
                        if (inboxFolder.Items[i] is Microsoft.Office.Interop.Outlook.MailItem)
                        {
    
                            Microsoft.Office.Interop.Outlook.MailItem item = (Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items[i];
    
                            MessageBox.Show(item.Subject);
    
                        }
                    }
                    catch (System.Runtime.InteropServices.COMException ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myApp);

    Please, marks as answer if helpful.


    • Edited by Deric Ferreira Thursday, October 30, 2014 11:22 AM
    • Marked as answer by Heinz09 Thursday, October 30, 2014 11:31 AM
    Thursday, October 30, 2014 11:22 AM
  • Just found it, that was exactly the problem, it tried to use a meeting request as an email and that is where it kept crashing.

    Thank you very much for pointing me in the right direction, I think things should be much smoother now.

    Thursday, October 30, 2014 11:31 AM