none
VB.NET Microsoft.Office.Interop.Outlook Task Scheduler COM class Failure RRS feed

  • Question

  • I have this VB.NET application that runs fine,  if I am logged into Window 10,  but if the application runs as a Task Scheduler it fails with the below error.  also note under windows 7 Task Scheduler it ran fine.   Per this article it has to do with COM system refuses to marshal calls between two processes running in different security contexts.      

    Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).

     Code that is failing under task scheduler: 
     Try
    
                Dim olapp As New Outlook.Application
                Dim fldInbox As Outlook.MAPIFolder = olapp.GetNamespace("mapi").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
                Dim SubFolder = fldInbox.Folders("Automation")
                Dim mail As Object
                Dim iCtr As Integer
                Dim iAttachCnt As Integer
                Dim sFileName As String
    
                For Each mail In SubFolder.Items
                    With mail.Attachments
                        Dim strSaveBy As String = mail.Subject
                        If strSaveBy.Contains("Subject Line") = True Then
                            iAttachCnt = .Count
                            If iAttachCnt > 0 Then
                                'if we have attachment
                                For iCtr = 1 To iAttachCnt
                                    Try
                                        'mail.UnRead = False
                                        sFileName = .Item(iCtr).FileName.ToString
                                        .Item(iCtr).SaveAsFile(LoadDir & "filename.xls")
    
                                        mail.Delete()
    
                                    Catch ex As System.Exception
                                        'do nothing
                                    End Try
                                Next iCtr
                            End If
                        End If
                    End With
    
                Next mail
    
                System.Runtime.InteropServices.Marshal.ReleaseComObject(fldInbox)
                While (System.Runtime.InteropServices.Marshal.ReleaseComObject(olapp) > 0)
                End While
                'System.Runtime.InteropServices.Marshal.ReleaseComObject(olapp)
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(olapp)
                olapp = Nothing
                olapp.Application.Quit()
                olapp.Quit()
    
            Catch ex As System.Exception
                'MsgBox(ex.Message)
                WriteLinetoLog(strLogPath, " OLeMailReader " & ex.Message)
            End Try
    any help is greatly appreciated

    Work Smarter Not Harder

    Friday, October 11, 2019 3:26 PM

All replies

  • Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    As a workaround you may consider using a low-level API on  which Outlook is based on - Extended MAPI. Or just any third-party wrappers around that API such as Redemption.

    If you deal with Exchange based profiles you may consider also using EWS, see Start using web services in Exchange for more information.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers


    Friday, October 11, 2019 3:43 PM
  • I tried Redemption and remarked out the 'Imports Microsoft.Office.Interop.Outlook it obviously did not work.  unsure what all needs to change in my code to make Redemption work.   I did not find a simple Redemption example to save an attachment.    

    


    Work Smarter Not Harder

    Friday, October 11, 2019 4:26 PM
  • let me know,  thanks Joel

    from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    As a workaround you may consider using a low-level API on  which Outlook is based on - Extended MAPI. Or just any third-party wrappers around that API such as Redemption.

    If you deal with Exchange based profiles you may consider also using EWS, see Start using web services in Exchange for more information.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers


    If "Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications
    " then why is there Interop?  ie 
    Microsoft.Office.Interop.Outlook.    If Redemption will work I need help or example code to read inbox or specific sub folder for a specific subject line contains x and open and save attachment.   

    other thoughts why is task scheduler that uses my credentials that runs .net app that call outlook interop stop working between Task Scheduler Windows 10 but worked in Windows 7.  sounds like a bug.  

    is there any workaround?  registry setting to allow?  please let me know.  


    Work Smarter Not Harder

    Friday, October 11, 2019 9:28 PM
  • Hello Jim,

    If "Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications " then why is there Interop?  ie Microsoft.Office.Interop.Outlook.

    Well, Microsoft does indeed. You can automate Microsoft applications from a software launched by users, but not from a service or web application. If you really need to support such kind of applications then you need to consider using a low-level API which supports multithreading. 


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Saturday, October 12, 2019 10:09 AM
  • You code will prety much stay the same - RDOSession in Redemption is very similar to Namespace in OOM. You can replace the following code

     Dim olapp As New Outlook.Application
                Dim fldInbox As Outlook.MAPIFolder = olapp.GetNamespace("mapi").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
                Dim SubFolder = fldInbox.Folders("Automation")

    with

    Dim session As New Redemption.RDOSesion

    session.Logon() Dim fldInbox As Redemption.RDOFolder = session..GetDefaultFolder(Redemption.rdoDefaultFolders.olFolderInbox)

    Dim SubFolder = fldInbox.Folders("Automation")


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by jimbrown.ws Monday, October 14, 2019 1:02 PM
    • Unmarked as answer by jimbrown.ws Monday, October 14, 2019 1:47 PM
    Saturday, October 12, 2019 12:27 PM
  • I get the same error "Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE))." When running in Task Scheduler,  code works if ran from Windows 10 UI  Winform.  any other thoughts?  and thank you for your help.   

    Code:  

     Try
    
                'Dim olapp As New Outlook.Application
                'Dim fldInbox As Outlook.MAPIFolder = olapp.GetNamespace("mapi").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
                'Dim SubFolder = fldInbox.Folders("Automation")
    
                Dim session As New Redemption.RDOSesion
    
                session.Logon()
                Dim fldInbox As Redemption.RDOFolder = session.GetDefaultFolder(Redemption.rdoDefaultFolders.olFolderInbox)
    
                Dim SubFolder = fldInbox.Folders("Automation")
    
    
                Dim mail As Object
                Dim iCtr As Integer
                Dim iAttachCnt As Integer
                Dim sFileName As String
    
                For Each mail In SubFolder.Items
                    With mail.Attachments
                        Dim strSaveBy As String = mail.Subject
                        If strSaveBy.Contains("Subject Line") = True Then
                            iAttachCnt = .Count
                            If iAttachCnt > 0 Then
                                'if we have attachment
                                For iCtr = 1 To iAttachCnt
                                    Try
                                        'mail.UnRead = False
                                        sFileName = .Item(iCtr).FileName.ToString
                                        .Item(iCtr).SaveAsFile(LoadDir & "filename.xls")
    
                                        mail.Delete()
    
                                    Catch ex As System.Exception
                                        'do nothing
                                    End Try
                                Next iCtr
                            End If
                        End If
                    End With
    
                Next mail
    
                System.Runtime.InteropServices.Marshal.ReleaseComObject(fldInbox)
                While (System.Runtime.InteropServices.Marshal.ReleaseComObject(olapp) > 0)
                End While
                'System.Runtime.InteropServices.Marshal.ReleaseComObject(olapp)
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(olapp)
                olapp = Nothing
                olapp.Application.Quit()
                olapp.Quit()
    
            Catch ex As System.Exception
                'MsgBox(ex.Message)
                WriteLinetoLog(strLogPath, " OLeMailReader " & ex.ToString)
            End Try
    


    Work Smarter Not Harder

    Saturday, October 12, 2019 4:25 PM
  • That error corresponds to a call creating an instance of the Outlook.Application object.

    Are you sure that is the error you are getting?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Saturday, October 12, 2019 4:31 PM
  • I don't know why, but I put the changes in and compiled and I have code above with no errors but now I get an error with Dim session As New Redemption.RDOSesion any suggestions?  


    Work Smarter Not Harder


    • Edited by jimbrown.ws Saturday, October 12, 2019 5:58 PM
    Saturday, October 12, 2019 5:53 PM
  • Just curious --

    In the task scheduler, is the account you are using a member of the Administrator's group?

    Is the task configured to run with highest privileges?

    Is the task configured to run when the user is not logged-on?

    Saturday, October 12, 2019 8:56 PM
  • Yes, a member of local Admin and Yes, Run highest privileges and also Yes, Run whether user is logged on or not.   

    Work Smarter Not Harder

    Saturday, October 12, 2019 9:25 PM
  • What happens If you uncheck the box for running when the user is not logged on?

    And why do you need to run the client with elevated privileges?

    Saturday, October 12, 2019 10:24 PM
  • It would run.  but I need the task to run at 4:00 am when I'm not logged in.  I guess I have not tried not checking elevated privileges.  but can on Monday when back in office.  

    Work Smarter Not Harder

    Saturday, October 12, 2019 10:38 PM
  • It would run.

    Does this mean that when the task scheduler executes the task that your application does not experience the COM error?

    When the task scheduler executes a task without the user being logged-on a batch logon is performed for the account used and the task runs in a non-interactive session. If I remember correctly, it will not have network access if you have not saved the account password.

    Also, in my experience, when the task scheduler runs a task without the user being logged on and the user account is a member of the Administrators group the task is run with elevated privileges, even if the highest privileges box is unchecked.   But I haven't tested this on Win 10.


    • Edited by RLWA32 Sunday, October 13, 2019 11:43 AM added information
    Sunday, October 13, 2019 9:49 AM
  • Did you add  the "imports" statement to the .vb file?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Sunday, October 13, 2019 11:18 AM
  • Yes,  it's grayed out.   

     

    Work Smarter Not Harder

    Sunday, October 13, 2019 2:17 PM
  • yes,  it will run correctly as it works when it runs in the Windows UI.    It runs if I load from Windows or as a task "Run only when user is logged on"   

    Work Smarter Not Harder

    Sunday, October 13, 2019 2:55 PM
  • yes,  it will run correctly as it works when it runs in the Windows UI.    It runs if I load from Windows or as a task "Run only when user is logged on"  

    Well, this would seem to indicate that running the task in a non-interactive session is the root of the problem.  This is in line with Eugene Astafiev's earlier comment regarding Microsoft's support for automation of Office applications.

    That something "worked" in Windows 7 is not necessarily indicative of a bug in a later version of Windows when it stops working.  It could be that the circumstances aren't really comparable or that Windows 7 behavior was buggy and it was corrected later on.

    Sunday, October 13, 2019 3:08 PM
  • Any suggestions to get Redemption  working,  as it looks like Windows 10 Task Scheduler is worthless.   

    Work Smarter Not Harder

    Sunday, October 13, 2019 8:01 PM
  • You need to add Redemption to the imports clause.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, October 14, 2019 12:37 AM
  • You need to add Redemption to the imports clause.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    I did it's grayed out.  

     

     

    Work Smarter Not Harder

    Monday, October 14, 2019 12:46 AM
  • It looks like "RDOSession" is misspelled.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by jimbrown.ws Monday, October 14, 2019 1:02 PM
    • Unmarked as answer by jimbrown.ws Monday, October 14, 2019 1:47 PM
    Monday, October 14, 2019 2:04 AM
  • That worked.  fixed compile error,   tested in Windows UI, but unable to test in Windows Task Scheduler as I get error 0xe0434352.   I believe the displays a message box is poping up and because Task Scheduler is not in the Windows UI I can't except popup.  thanks for the help.    


    Work Smarter Not Harder



    • Edited by jimbrown.ws Monday, October 14, 2019 1:55 PM
    Monday, October 14, 2019 1:02 PM