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:
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
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
For vIndex As Integer = TotalOutlookMessages To 1 Step -1
Dim DeleteEmailMessageOK As Boolean = False
oMessage = oFldr.Items(vIndex)
RaiseEvent NewMsg(oMessage)
With oMessage
iMsgCount = iMsgCount + 1
LineTrace = 12
If oMessage.Subject.IndexOf(sSubjectLineToFind) = 0 Then
LineTrace = 13
DeleteEmailMessageOK = True
With oMessage.Attachments
LineTrace = 14
iAttachCnt = .Count
If iAttachCnt > 0 Then
LineTrace = 15
For iCtr = 1 To iAttachCnt
LineTrace = 16
.Item(iCtr).SaveAsFile(DataIncomingFolder & .Item(iCtr).FileName)
DeleteEmailMessageOK = DeleteEmailMessageOK And CheckFileOK(DataIncomingFolder & .Item(iCtr).FileName)
RaiseEvent NHDS_Data_Msg_Found(oMessage, oMessage.Attachments.Item(iCtr))
Next iCtr
Else
LineTrace = 17
RaiseEvent NHDS_Data_Msg_Found(oMessage, Nothing)
End If
End With
End If
End With
LineTrace = 18
System.Windows.Forms.Application.DoEvents()
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
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:
Catch ex As System.Runtime.InteropServices.COMException