Number of messages in Inbox using VBA-code

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
siepeltukker
Newbie
Posts: 2
Joined: Fri May 13, 2005 12:09 pm
Location: Ootmarsum, Netherlands

Number of messages in Inbox using VBA-code

Post by siepeltukker » Fri May 13, 2005 12:21 pm

In the inbox of Outlook 2003 you can eiter see the number of unread messages or (by changing some settings) the total number of messages.

I would like to use a macro to take action in case a message exists in the Inbox, read and unread messages. In case there are messages (count 0) take action based on the emailmessage, in case there are no messages (count = 0) close Outlook and stop the macro.

Can somebody help me creating such a macro?

Many thanks in advance.

Nico Leussink.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri May 13, 2005 12:35 pm

Hi,

Might want to check out the documentation:
http://msdn.microsoft.com/library/defau ... mcount.asp

So probably want something like:

VBSTART
Function CountUnReadMsgs
Dim myOlApp
Dim myOlNameSpace
Dim objFolder
Dim itms
Dim ItemCount, UnReadItemCount

Set myOlApp = CreateObject("Outlook.Application")
Set myOlNameSpace = myOlApp.GetNamespace("MAPI")
Set objFolder = myOlNameSpace.GetDefaultFolder(6)
Set itms = objfolder.Items
ItemCount = itms.Count
'ItemCount is total number of messages

UnReadItemCount = objFolder.UnReadItemCount
CountUnReadMsgs = UnReadItemcount

End Function
VBEND
VBEval>CountUnReadMsgs,NumUnRead

This just counts unread messages. It is untested. Just off the cuff. Need to determine how to get the correct folder. See msdn.microsoft.com for Outlook scripting docs.

siepeltukker
Newbie
Posts: 2
Joined: Fri May 13, 2005 12:09 pm
Location: Ootmarsum, Netherlands

Post by siepeltukker » Fri May 13, 2005 1:17 pm

This works, thanks! And many thanks for the quick responce!

Just made a very small change in your script for the total number of items in the inbox. In your script the number of unread messages is displayed, in the below script the total (read and unread) number of messages is displayed.

VBSTART
Function CountMsgs
Dim myOlApp
Dim myOlNameSpace
Dim objFolder
Dim itms
Dim ItemCount, UnReadItemCount

Set myOlApp = CreateObject("Outlook.Application")
Set myOlNameSpace = myOlApp.GetNamespace("MAPI")
Set objFolder = myOlNameSpace.GetDefaultFolder(6)
Set itms = objfolder.Items
CountMsgs = itms.Count
'ItemCount is total number of messages

End Function
VBEND
VBEval>CountMsgs,NumMessages

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts