Getting a Count of Unread Email in Outlook
Last post I was talking about DataSets and aggregating data with LINQ and the example I gave was around finding rows of data where the Status = “Unread”. This got me thinking about how we could do this by asking Outlook for my unread email count – one of the programs I spend a lot of time in ;-). This actually is a pretty easy thing to do if you use the Primary Interop Assembly (PIA) for Outlook – Microsoft.Office.Interop.Outlook. Add a reference to your project and then import the namespace at the top of your program.
Imports Microsoft.Office.Interop.Outlook
Then you can write the following code:
Dim OutlookApp As New Application()
Dim myMail As [NameSpace] = OutlookApp.GetNamespace("MAPI")
myMail.Logon(ShowDialog:=True, NewSession:=False)
Dim count As Integer = myMail.GetDefaultFolder(OlDefaultFolders.olFolderInbox).UnReadItemCount
Note that if you have selected an Outlook Add-in project template in Visual Studio then you already have reference to the Application and you’re already logged in so the code is even simpler:
Dim myMail As [NameSpace] = Me.Application.GetNamespace("MAPI")
Dim unread As Integer = myMail.GetDefaultFolder(OlDefaultFolders.olFolderInbox).UnReadItemCount
Enjoy!
Comments
- Anonymous
April 28, 2009
PingBack from http://microsoft-sharepoint.simplynetdev.com/getting-a-count-of-unread-email-in-outlook/