none
Getting - Unable to cast object of type 'System.__ComObject' to type 'Microsoft.Office.Interop.Outlook.ApplicationClass RRS feed

  • Question

  • First of all this is in a winforms application.  The user has the ability to look into their outlook folders and save them to a network location as .msg. For most users the error does not occur, however, it does happen to a few.  Here is a code snippet:

    If MailDataToDel.DeleteMail Then

                            Dim OutlookApp As Microsoft.Office.Interop.Outlook.Application

                            OutlookApp = UtilModules.GetOutlookProc()

     

                            Dim OutlookNS As Microsoft.Office.Interop.Outlook.NameSpace = _

                                OutlookApp.GetNamespace("MAPI")

                            If OutlookNS.GetItemFromID(MailDataToDel.EntryID) IsNot Nothing Then

                                Dim Del_MailItem As Microsoft.Office.Interop.Outlook.MailItem = _

                                    CType(OutlookNS.GetItemFromID(MailDataToDel.EmailItem.EntryID), _

                                    Microsoft.Office.Interop.Outlook.MailItem)

                                Try

                                    Del_MailItem.Delete()

                                Catch DelMailEX As Exception

                                    PreserveError(DelMailEX)

                                Finally

                                    System.Runtime.InteropServices.Marshal.ReleaseComObject(OutlookApp)

                                    System.Runtime.InteropServices.Marshal.ReleaseComObject(OutlookNS)

                                    System.Runtime.InteropServices.Marshal.ReleaseComObject(Del_MailItem)

                                    GC.WaitForPendingFinalizers()

                                    GC.Collect()

                                End Try

                            End If

                        End If

                    End If

     

    Does anybody have an idea why this happens and then only to a select few?

     

    Friday, January 20, 2012 8:43 PM

Answers

  • Hi Silient-Knight,

    Here are some things you could try:

     

    1. Change your code to always create a new Outlook.Application.  Outlook is a singleton therefore there should be only one instance of Outlook running at a time.  COM will ensure that you always get back the ActiveObject regardless of whether the process is running or not.
    2. Change your code to return the interface instead of the class.  For example change the method signature of GetOutlookProc from this:
      Public Function GetOutlookProc As Microsoft.Office.Interop.Outlook.Application
         ...
      End Function
      

       

      to this:

      Public Function GetOutlookProc As Microsoft.Office.Interop.Outlook._Application
         ...
      End Function
      
    3. Get a ProcMon on a working machine and a broken machine during the time that the error is occuring.  Look for missing registry keys in the broken case.
    Dave
    Please make sure to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Silent-Knight Friday, February 17, 2012 7:55 PM
    Wednesday, January 25, 2012 3:52 PM

All replies

  • Hi Silent-Knight,

     

    Thanks for posting in the MSDN Forum.

     

    After review the title of your post, I don’t think the snippet will make more help for trouble-shooting. It’s based on my experience that you have error when you create an instance for Outlook application interface. So the method “UtilModules.GetOutlookProce()” is this issue’s key. I’d recommend you explain that method with more detailed information.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 23, 2012 7:04 AM
    Moderator
  • Tom,

    Thank you for your response.  Here is the method:

     

    Function GetOutlookProc() As Microsoft.Office.Interop.Outlook.Application

     

        Dim AppProcess() As System.Diagnostics.Process = Process.GetProcessesByName("outlook")

     

        Dim myOlApp As Microsoft.Office.Interop.Outlook.Application

     

        Try

     

            If AppProcess.GetLength(0) > 0 Then

                myOlApp = System.Runtime.InteropServices.Marshal.GetActiveObject(

    "Outlook.Application")

     

            Else

                myOlApp =

    New Microsoft.Office.Interop.Outlook.Application

     

            End If

    'Attempt to create the outlook application if there is an exception.

     

        Catch AppCommEX As System.Runtime.InteropServices.COMException

            myOlApp =

    New Microsoft.Office.Interop.Outlook.Application

     

        Catch olAppEX As Exception

            myOlApp =

    New Microsoft.Office.Interop.Outlook.Application

     

        End Try

        System.Threading.Thread.Sleep(1000)

        GetOutlookProc = myOlApp

     

    End Function

     

    Monday, January 23, 2012 1:35 PM
  • Hi Silent-Knight,

     

    It’s based on my experience that there has no error in your snippet. I’m wondering why it had exception too. I will involve some experts into this thread. I hope them can solved your issue. There might be some time delay. Appreciate your patience.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 24, 2012 4:03 AM
    Moderator
  • How do you reference office types in your project? from PIA assembly or in some different way?

    Tuesday, January 24, 2012 9:56 AM
  • Hi Silient-Knight,

    Here are some things you could try:

     

    1. Change your code to always create a new Outlook.Application.  Outlook is a singleton therefore there should be only one instance of Outlook running at a time.  COM will ensure that you always get back the ActiveObject regardless of whether the process is running or not.
    2. Change your code to return the interface instead of the class.  For example change the method signature of GetOutlookProc from this:
      Public Function GetOutlookProc As Microsoft.Office.Interop.Outlook.Application
         ...
      End Function
      

       

      to this:

      Public Function GetOutlookProc As Microsoft.Office.Interop.Outlook._Application
         ...
      End Function
      
    3. Get a ProcMon on a working machine and a broken machine during the time that the error is occuring.  Look for missing registry keys in the broken case.
    Dave
    Please make sure to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Silent-Knight Friday, February 17, 2012 7:55 PM
    Wednesday, January 25, 2012 3:52 PM