Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Getting # of unread mails and alarms using cdo

Posted on 2000-05-09
10
Medium Priority
?
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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 1600 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

636 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