Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was hoping that after reading the about all the amazing code people have posted here, that someone can help me with my simplistic VB.Net app.

My app scans the Outlook Inbox looking for specific subject lines. If Outlook is open while it is scanning the Inbox, and I close Outlook, it crashes. Here's my code:
VB
Public Sub ProcessInbox()
        Dim LineTrace As Decimal = 0

        Dim oOutlook As New Microsoft.Office.Interop.Outlook.Application
        LineTrace = 1
        Dim oNs As Microsoft.Office.Interop.Outlook.NameSpace
        LineTrace = 2
        Dim oFldr As Microsoft.Office.Interop.Outlook.MAPIFolder
        LineTrace = 3
        Dim oAttachments As Microsoft.Office.Interop.Outlook.Attachments
        LineTrace = 4
        Dim oAttachment As Microsoft.Office.Interop.Outlook.Attachment
        LineTrace = 5
        Dim oMessage As Object

        Try
            Dim iMsgCount As Integer
            Dim iCtr, iAttachCnt As Short
            LineTrace = 8
            Dim sSubjectLineToFind As String = "ReAsure_HealthNode_"
            LineTrace = 9
            Dim DataIncomingFolder As String = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal) & _
                IncommingDataFolder
            LineTrace = 10

            mBusy = True        ' Indicate that the scan is active so that
            '                   ' no other scans are also initiated.
            'get reference to inbox
            oNs = oOutlook.GetNamespace("MAPI")
            LineTrace = 10.5
            oFldr = oNs.GetDefaultFolder(6)
            LineTrace = 10.8
            Dim TotalOutlookMessages As Integer = oFldr.Items.Count
            RaiseEvent MsgCount(TotalOutlookMessages)

            LineTrace = 11
            'System.Diagnostics.Debug.WriteLine("Start- Enumerating all Inbox messages:")
            'For Each oMessage In oFldr.Items
            '    Console.WriteLine(oMessage.Subject)
            'Next oMessage
            'System.Diagnostics.Debug.WriteLine("End- Enumerating all Inbox messages:")

            For vIndex As Integer = TotalOutlookMessages To 1 Step -1
                Dim DeleteEmailMessageOK As Boolean = False
                oMessage = oFldr.Items(vIndex)
                RaiseEvent NewMsg(oMessage)
                With oMessage
                    'basic info about message
                    'System.Diagnostics.Debug.WriteLine(.To)
                    'System.Diagnostics.Debug.WriteLine(.CC)
                    'Console.WriteLine("Current message subject is: " & .Subject)
                    'System.Diagnostics.Debug.WriteLine(.Body)
                    'If .UnRead Then
                    '    'System.Diagnostics.Debug.WriteLine("Message has not been read")
                    'Else
                    '    'System.Diagnostics.Debug.WriteLine("Message has been read")
                    'End If
                    iMsgCount = iMsgCount + 1

                    ' oMessage = Nothing

                    LineTrace = 12

                    ' Look for specific subject lines in emails.
                    ' Console.WriteLine(oMessage.Subject.IndexOf(sSubjectLineToFind))
                    If oMessage.Subject.IndexOf(sSubjectLineToFind) = 0 Then
                        ' Found in this email!

                        LineTrace = 13

                        DeleteEmailMessageOK = True
                        With oMessage.Attachments
                            ' Any attachments?

                            LineTrace = 14

                            iAttachCnt = .Count
                            If iAttachCnt > 0 Then
                                ' Attachment(s) found.
                                ' Cycle thru each one.

                                LineTrace = 15

                                For iCtr = 1 To iAttachCnt
                                    ' Save this attachment.

                                    LineTrace = 16
                                    .Item(iCtr).SaveAsFile(DataIncomingFolder & .Item(iCtr).FileName)
                                    ' Check that the file was successfully saved.
                                    ' If OK, flag to delete the parent email.
                                    DeleteEmailMessageOK = DeleteEmailMessageOK And CheckFileOK(DataIncomingFolder & .Item(iCtr).FileName)
                                    ' Record the details of the attachment.
                                    RaiseEvent NHDS_Data_Msg_Found(oMessage, oMessage.Attachments.Item(iCtr))
                                Next iCtr
                            Else
                                ' Found an email but no attachments.
                                ' Record these details.

                                LineTrace = 17

                                RaiseEvent NHDS_Data_Msg_Found(oMessage, Nothing)
                            End If
                        End With
                    End If
                End With

                LineTrace = 18

                System.Windows.Forms.Application.DoEvents()

                ' Check that the file was successfully saved.
                ' If OK, delete the parent email.
                If DeleteEmailMessageOK Then

                    LineTrace = 19

                    Dim vDeletedMessageSubject As String = oMessage.subject
                    oMessage.Delete()
                    Console.WriteLine("Message with subject: {0} deleted.", vDeletedMessageSubject)
                End If
            Next vIndex

        Catch ex As System.Runtime.InteropServices.COMException
            ' Disregard this error. It occurs when Outlook is closed while the TN is scanning the Inbox.

        Catch ex As Exception
            Dim s As String = "Error [" & Err.Number & "] occured in the ProcessInbox module. Failure occured at step = " & LineTrace & vbNewLine & _
                "Error Desc: " & Err.Description
            Dim t As String = "Outlook Not Ready"
            Call frmScanInbox.SetTrayNotifyIconBalloonText(5000, s, t, ToolTipIcon.Warning)
            Console.WriteLine(s)

            mBusy = False

        Finally
            LineTrace = 20

            mBusy = False

            oOutlook.Quit()
            Marshal.ReleaseComObject(oOutlook)
            GC.Collect()
            oAttachment = Nothing
            oAttachments = Nothing
            oMessage = Nothing
            oFldr = Nothing
            oNs = Nothing
            oOutlook = Nothing
        End Try

    End Sub

To replicate the issue, I run my app which scans the Outlook Inbox. If I open Outlook manually, everything is OK. If my app is in the middle of scanning the inbox, and I manually close Outlook which I opened earlier, Outlook disappears from the Task Manager.

This seems to cause my app to crash, but it does not display an error, it just hangs. It also leaves a running instance of Outlook in the Task Manager.

If I kill the instance of Outlook from the Task Manger, my app suddenly continues running as if nothing has happened.

Can someone suggest why this is happening and how to fix it. I am at the point where I have no more ideas to try. Thank you.

Oh, forgot to mention. Once I kill the remaining Outlook task, the next line that is executed in my code is:
VB
Catch ex As System.Runtime.InteropServices.COMException
Posted
Updated 13-Oct-10 1:31am
v3

Try handling the Quit event on Microsoft.Office.Interop.Outlook.Application

see events section
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.application_members.aspx[^]
 
Share this answer
 
With this new information, is it possible for me to:

1. Detect when the user closes Outlook
2. Freeze the closing action of Outlook until my code finishes its scan.
3. If the user closed Outlook in step 1 above, close it now.

Would it be too much to ask for even sample code on how to do this bearing in mind my code above? I've never done this sort of work before with Outlook automation and it's very confusing. Thanks.

PS. From what I read, the Quit event might be too late in the Outlook closing cycle. I read that I may have to use the Close event. I would appreciate your help with this.
 
Share this answer
 
Sorry, but does anyone know how to detect Outlook closing from a VB.Net app? Sample code would be appreciated. Thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900