Solved

Getting # of unread mails and alarms using cdo

Posted on 2000-05-09
10
292 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

707 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

13 Experts available now in Live!

Get 1:1 Help Now