Solved

Getting # of unread mails and alarms using cdo

Posted on 2000-05-09
10
297 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

696 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