M
mlafarlett
Any clues on how to resolve memory leak?
Imports Outlook = Microsoft.Office.Interop.Outlook
Module subMain
Private mOutlookApp As Outlook.Application
Private mbContinue As Boolean = True
Sub main()
'Setting mOutlookApp here causes GUI to hang so I declare in
fnProcessInbox (see notes below)
'Try
' mOutlookApp = New Outlook.Application
'Catch ex As Exception
' MsgBox("fnProcessInbox...Err creating Outlook.Application"
& ex.Message)
' Exit Sub
'End Try
fnProcessInbox("D")
AddHandler mOutlookApp.NewMail, AddressOf eventhandlerNewMail
SleepForAwhile:
If mbContinue = True Then
System.Threading.Thread.Sleep(15000)
GoTo SleepForAwhile
End If
ShutDown:
mOutlookApp = Nothing
End
End Sub
Public Sub eventhandlerNewMail()
fnProcessInbox("Q")
End Sub
Private Sub fnProcessInbox(ByVal psTransactionCode As String)
Dim bTradeFile As Boolean = False
Dim oNS As Outlook.NameSpace
Dim oInbox As Outlook.MAPIFolder
'I SORT OF SUSPECT LEAK TO BE HERE BUT IF I MOVE TO LOCATION
ABOVE, the OUTLOOK GUI HANGS UP.
'SURPRISED THE EVENT HANDLER WORKS WHEN I CONTINUALLY
RE-DECLARE mOutlookApp but it does.
Try
mOutlookApp = New Outlook.Application
Catch ex As Exception
MsgBox("fnProcessInbox...Err creating Outlook.Application"
& ex.Message)
mbContinue = False
Exit Sub
End Try
Try
oNS = mOutlookApp.GetNamespace("mapi")
'Get Mapi NameSpace.
Catch ex As Exception
MsgBox("fnProcessInbox...Err establishing Outlook
namespace" & ex.Message)
mbContinue = False
Exit Sub
End Try
Try
oInbox =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) 'Get
Messages collection of Inbox.
Catch ex As Exception
MsgBox("fnProcessInbox...Err obtaining Outlook Inbox
collection" & ex.Message)
mbContinue = False
Exit Sub
End Try
Try
Dim oItems As Outlook.Items = oInbox.Items
Dim oMailItem As Object
Dim ix As Integer
Dim iCnt As Integer
iCnt = oItems.Count
Dim Attch As Object
Dim sFileName As String
For ix = iCnt To 1 Step -1
oMailItem = oItems.Item(ix)
If oMailItem.Class <> Outlook.OlObjectClass.olMail Then
GoTo SkipIt
End If
'Not sure how this happens but it does...think its when
an item is deleted the index gets screwed
If oMailItem.Subject Is Nothing Then
GoTo SkipIt
End If
If oMailItem.Subject.ToLower.IndexOf("test") > -1 Then
Try
oItems.Remove(ix)
Catch ex As Exception
MsgBox("fnProcessInbox...Error deleting email =
" & ex.Message)
mbContinue = False
End Try
End If
SkipIt:
Next
oItems = Nothing
oMailItem = Nothing
oInbox = Nothing
oMailItem = Nothing
oNS = Nothing
Catch ex As Exception
MsgBox(String.Concat("fnProcessInbox...", "Transaction Code
= ", psTransactionCode, " ex.message = ", ex.Message))
mbContinue = False
Exit Sub
End Try
End Sub
End Module
Imports Outlook = Microsoft.Office.Interop.Outlook
Module subMain
Private mOutlookApp As Outlook.Application
Private mbContinue As Boolean = True
Sub main()
'Setting mOutlookApp here causes GUI to hang so I declare in
fnProcessInbox (see notes below)
'Try
' mOutlookApp = New Outlook.Application
'Catch ex As Exception
' MsgBox("fnProcessInbox...Err creating Outlook.Application"
& ex.Message)
' Exit Sub
'End Try
fnProcessInbox("D")
AddHandler mOutlookApp.NewMail, AddressOf eventhandlerNewMail
SleepForAwhile:
If mbContinue = True Then
System.Threading.Thread.Sleep(15000)
GoTo SleepForAwhile
End If
ShutDown:
mOutlookApp = Nothing
End
End Sub
Public Sub eventhandlerNewMail()
fnProcessInbox("Q")
End Sub
Private Sub fnProcessInbox(ByVal psTransactionCode As String)
Dim bTradeFile As Boolean = False
Dim oNS As Outlook.NameSpace
Dim oInbox As Outlook.MAPIFolder
'I SORT OF SUSPECT LEAK TO BE HERE BUT IF I MOVE TO LOCATION
ABOVE, the OUTLOOK GUI HANGS UP.
'SURPRISED THE EVENT HANDLER WORKS WHEN I CONTINUALLY
RE-DECLARE mOutlookApp but it does.
Try
mOutlookApp = New Outlook.Application
Catch ex As Exception
MsgBox("fnProcessInbox...Err creating Outlook.Application"
& ex.Message)
mbContinue = False
Exit Sub
End Try
Try
oNS = mOutlookApp.GetNamespace("mapi")
'Get Mapi NameSpace.
Catch ex As Exception
MsgBox("fnProcessInbox...Err establishing Outlook
namespace" & ex.Message)
mbContinue = False
Exit Sub
End Try
Try
oInbox =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) 'Get
Messages collection of Inbox.
Catch ex As Exception
MsgBox("fnProcessInbox...Err obtaining Outlook Inbox
collection" & ex.Message)
mbContinue = False
Exit Sub
End Try
Try
Dim oItems As Outlook.Items = oInbox.Items
Dim oMailItem As Object
Dim ix As Integer
Dim iCnt As Integer
iCnt = oItems.Count
Dim Attch As Object
Dim sFileName As String
For ix = iCnt To 1 Step -1
oMailItem = oItems.Item(ix)
If oMailItem.Class <> Outlook.OlObjectClass.olMail Then
GoTo SkipIt
End If
'Not sure how this happens but it does...think its when
an item is deleted the index gets screwed
If oMailItem.Subject Is Nothing Then
GoTo SkipIt
End If
If oMailItem.Subject.ToLower.IndexOf("test") > -1 Then
Try
oItems.Remove(ix)
Catch ex As Exception
MsgBox("fnProcessInbox...Error deleting email =
" & ex.Message)
mbContinue = False
End Try
End If
SkipIt:
Next
oItems = Nothing
oMailItem = Nothing
oInbox = Nothing
oMailItem = Nothing
oNS = Nothing
Catch ex As Exception
MsgBox(String.Concat("fnProcessInbox...", "Transaction Code
= ", psTransactionCode, " ex.message = ", ex.Message))
mbContinue = False
Exit Sub
End Try
End Sub
End Module