Outlook MAPI - Find receive date of the oldest mail in a certain folder
Hi guys,
I'm faced with a weird error message in my code. Any idea how to deal with this in a more efficient way?
The goal is the following:
For many different Outlook folders, I need to display the receive date of the oldest mail. I'm currently using the following code:
General Declarations + default values
Code:
Dim oOutlook As Microsoft.Office.Interop.Outlook.Application
Dim oNs As Microsoft.Office.Interop.Outlook.NameSpace
Dim oFldr As Microsoft.Office.Interop.Outlook.MAPIFolder
Dim blnFinishedWorking As Boolean = True
Dim Profiles(8, 0) As String
Profiles(0, 0) = "Inbox"
Sub to retrieve the values is being called from the below timer with a user adjustable interval.
Code:
Private Sub ViewUnreadMessages(ByVal FolderIndex As Integer)
'Open Outlook
oOutlook = New Microsoft.Office.Interop.Outlook.Application
oNs = oOutlook.GetNamespace("MAPI")
'Put the Outlook foldername in a variable
Dim OutlookFolder As String = Profiles(0, FolderIndex)
'Depending on whether the folder is the main Inbox or an Inbox subfolder, use the appropriate code to open it
If OutlookFolder = "Inbox" Then
oFldr = oNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox) 'If the folder is called Inbox, then just check the inbox
Else
oFldr = oNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox).Folders(OutlookFolder) 'If the folder is a subfolder of the Inbox, then just check that folder
End If
'Put the amount of unread mail on the UnreadLabel
UnreadLabel(FolderIndex).Text = oFldr.UnReadItemCount
'Then track down the oldest mail and store it in variable OldestEmail
Dim OldestEmail As Date = Now()
'This is the For Loop causing the slowdown/crash
For Each Email In oFldr.Items
If Email.UnRead = True Then
If Email.ReceivedTime < OldestEmail Then OldestEmail = Email.ReceivedTime
End If
Next
'Calculate the delay in minutes
Dim LngDelayInMinutes As Long = DateDiff(DateInterval.Minute, OldestEmail, Now())
'Convert the delay to an understandable hh:mm format
Dim Minutes = Fix(LngDelayInMinutes)
Dim Hours = Fix(LngDelayInMinutes / 60)
Minutes = LngDelayInMinutes - (Hours * 60)
If Minutes < 10 Then
DelayLabel(FolderIndex).Text = CStr(Hours) & ":0" & CStr(Minutes) 'leading zero if amount of minutes is just 1 character
Else
DelayLabel(FolderIndex).Text = CStr(Hours) & ":" & CStr(Minutes)
End If
'Close Outlook
oFldr = Nothing
oNs = Nothing
oOutlook = Nothing
blnFinishedWorking = True
End Sub
This is the timer code that calls the sub for each Outlook folder
Code:
If blnFinishedWorking = True Then
'For each Outlook Folder, put the amount of unread messages in the appropriate label
For i = 0 To Profiles.GetUpperBound(1)
ViewUnreadMessages(i)
Next
End If
This code works fine as long as there are not too many emails in the Outlook folder. But in my environment, it causes serious application slowness and even crashes. Is there a way to make the offending loop lighter so it doesn't crash the app?
Error: Error HRESULT E_FAIL has been returned from a call to a COM component.
Error code: -2147467259
Debug code:
Code:
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'F:\TEST\Unread Mail Project\Unread Mail\Unread Mail\bin\Debug\Unread Mail.vshost.exe', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Deployment\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Deployment.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.VisualBasic\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualBasic.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread 'vshost.NotifyLoad' (0x1624) has exited with code 0 (0x0).
The thread '<No Name>' (0x1194) has exited with code 0 (0x0).
The thread '<No Name>' (0x3ec) has exited with code 0 (0x0).
The thread '<No Name>' (0xa14) has exited with code 0 (0x0).
The thread 'vshost.LoadReference' (0x1b8) has exited with code 0 (0x0).
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'F:\TEST\Unread Mail Project\Unread Mail\Unread Mail\bin\Debug\Unread Mail.exe', Symbols loaded.
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.Remoting\v4.0_4.0.0.0__b77a5c561934e089\System.Runtime.Remoting.dll'
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\assembly\GAC\Microsoft.Office.Interop.Outlook\11.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Outlook.dll'
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\assembly\GAC\office\11.0.0.0__71e9bce111e9429c\office.dll'
'Unread Mail.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\CustomMarshalers\v4.0_4.0.0.0__b03f5f7f11d50a3a\CustomMarshalers.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.VisualBasic.dll
The program '[2088] Unread Mail.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).
Many thanks for your insight!