Solved

Getting # of unread mails and alarms using cdo

Posted on 2000-05-09
10
293 Views
Last Modified: 2008-03-03
Hi!

How can I get the number of all unread mails and all currently active or waiting alarms from the calender?

Using CDO I have to loop through all messages; I ask for an easier and faster way.
0
Comment
Question by:Geffl
  • 5
  • 5
10 Comments
 
LVL 22

Expert Comment

by:ture
ID: 2799273
I'm not quite sure what we are talking about here... Is it Outlook 2000?
0
 
LVL 1

Author Comment

by:Geffl
ID: 2799383
The clients have Outlook 2000 installed, but I want to access the information from a VB program (Screensaver).

Actually I solved the problem already using loops and filters. This isn´t really a fast way. I hope there is a way to do it faster.
0
 
LVL 22

Expert Comment

by:ture
ID: 2799430
This is Outlook 2000 code. If you need help adapting it to VB, please ask again.

It counts all unread items in all mail folders and all appointment folders.

Sub CountUnreadItems()
  Dim ns As NameSpace
  Dim f As MAPIFolder
  Dim UnreadMails As Long
  Dim UnreadAppointments As Long
   
  Set ns = GetNamespace("MAPI")
 
  For Each f In ns.Folders(1).Folders
    If f.DefaultItemType = olMailItem Then
      UnreadMails = UnreadMails + f.UnReadItemCount
    ElseIf f.DefaultItemType = olAppointmentItem Then
      UnreadAppointments = UnreadAppointments + f.UnReadItemCount
    End If
  Next f

  MsgBox UnreadMails
  MsgBox UnreadAppointments
End Sub

/Ture
0
 
LVL 22

Expert Comment

by:ture
ID: 2799613
Geffl,

Here is the VB version. I believe that it does exactly what you asked for.

Private Sub Command1_Click()
  'This procedure needs a reference to the
  'Microsoft Outlook 9.0 Object Library
  Dim ol As Outlook.Application
  Dim ns As Outlook.NameSpace
  Dim f As Outlook.MAPIFolder
  Dim CloseOutlook As Boolean
  Dim UnreadMails As Long
  Dim UnreadAppointments As Long
   
  'Try using existing instance of Outlook
  On Error Resume Next
  Set ol = GetObject(Class:="Outlook.Application")

  'If Outlook was not running, start it
  If ol Is Nothing Then
    Set ol = CreateObject(Class:="Outlook.Application")
    CloseOutlook = True
  End If
 
  On Error GoTo 0
 
  'If Outlook is still not running, quit
  If ol Is Nothing Then Exit Sub
   
  'Set namespace
  Set ns = ol.GetNamespace("MAPI")
 
  'Loop through all folders and find unreadcount
  For Each f In ns.Folders(1).Folders
    If f.DefaultItemType = olMailItem Then
      UnreadMails = UnreadMails + f.UnReadItemCount
    ElseIf f.DefaultItemType = olAppointmentItem Then
      UnreadAppointments = UnreadAppointments + f.UnReadItemCount
    End If
  Next f
 
  'Show results
  MsgBox UnreadMails
  MsgBox UnreadAppointments
 
  'Close outlook if it has been started by this application
  If CloseOutlook Then ol.Quit
 
  'Release object variables
  Set ol = Nothing
  Set ns = Nothing
  Set f = Nothing
End Sub

/Ture
0
 
LVL 1

Author Comment

by:Geffl
ID: 2799979
Thanks for your reply!

As I wrote before, I already implemented a similar code to get all unread messages (I don´t use Outlook OLE, I use CDO). But for big mailboxes (700MB) this is very slow. I optimized it with the Filter on the Messages collection (Filtered all Messages with Unread property true). But I still can´t believe that this is the only (slow) way.
Unfortunatly it seems the unread property don´t affect appointmentitems. So I have to lopp through all appointment items (filtered the messages collection with ReminderSet-property true) and calculate myself if an item is overdue.

Actually I already have a working code, but I´m not pleased.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:ture
ID: 2800788
Geffl,

I'm not quite sure which appointments are considered 'unread'. If you only adjust the filter string to whatever you prefer, this procedure should work without too much looping...

Private Sub Command1_Click()
  'This procedure needs a reference to the
  'Microsoft Outlook 9.0 Object Library
  Dim ol As Outlook.Application
  Dim ns As Outlook.NameSpace
  Dim f As Outlook.MAPIFolder
  Dim FilterString As String
  Dim appt As Outlook.AppointmentItem
  Dim CloseOutlook As Boolean
  Dim UnreadMails As Long
  Dim UnreadAppointments As Long
 
  'Try using existing instance of Outlook
  On Error Resume Next
  Set ol = GetObject(Class:="Outlook.Application")

  'If Outlook was not running, start it
  If ol Is Nothing Then
    Set ol = CreateObject(Class:="Outlook.Application")
    CloseOutlook = True
  End If
   
  On Error GoTo 0
   
  'If Outlook is still not running, quit
  If ol Is Nothing Then Exit Sub
     
  'Set namespace
  Set ns = ol.GetNamespace("MAPI")
   
  'Loop through all mail folders and find unreadcount
  For Each f In ns.Folders(1).Folders
    If f.DefaultItemType = olMailItem Then
      UnreadMails = UnreadMails + f.UnReadItemCount
    End If
  Next f
   
  'Filter out appointments with reminder and Start date <= today
  FilterString = ""
  FilterString = "[Reminder] = ""Yes"""
  FilterString = FilterString & " and"
  FilterString = FilterString & " [Start] <= """ & Format(Now, "General Date") & """"
 
  Set f = ns.GetDefaultFolder(olFolderCalendar)
  Set appt = f.Items.Find(Filter:=FilterString)
  Do Until appt Is Nothing
    UnreadAppointments = UnreadAppointments + 1
    Set appt = f.Items.FindNext
  Loop
 
  'Show results
  MsgBox UnreadMails
  MsgBox UnreadAppointments
   
  'Close outlook if it has been started by this application
  If CloseOutlook Then ol.Quit
   
  'Release object variables
  Set ol = Nothing
  Set ns = Nothing
  Set f = Nothing
End Sub

/Ture
0
 
LVL 1

Author Comment

by:Geffl
ID: 2813655
Hi TURE!

I see, there is no 'perfect' solution. I implemented a similar solution like yours. Anyway, I thank for your advice and wait for an answer to give you my points.

Maybe you can give me a hint for an other problem I have with Outlook: In the company I work for, I have to give all users two icons in the toolbar to open a template. How the heck can I automatically (programmatically) all users these two buttons. Is there a way?

Thanx
Geffl
0
 
LVL 1

Author Comment

by:Geffl
ID: 2813657
Hi TURE!

I see, there is no 'perfect' solution. I implemented a similar solution like yours. Anyway, I thank for your advice and wait for an answer to give you my points.

Maybe you can give me a hint for an other problem I have with Outlook: In the company I work for, I have to give all users two icons in the toolbar to open a template. How the heck can I automatically (programmatically) all users these two buttons. Is there a way?

Thanx
Geffl
0
 
LVL 22

Accepted Solution

by:
ture earned 400 total points
ID: 2813765
Geffl,

Upgrading to answer.

I'm sorry, but I don't know how to put icons into the toolbar. I believe that this could be done using the Windows Scripting Host (which is a very powerful replacement for the old DOS batch files), but I haven't used that at all.

Here is a link that may provide you with some useful information:
http://msdn.microsoft.com/scripting/

/Ture
0
 
LVL 1

Author Comment

by:Geffl
ID: 2813899
Again, thanx a lot ...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now