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.
Number of messages in Inbox using VBA-code
Moderators: JRL, Dorian (MJT support)
-
- Newbie
- Posts: 2
- Joined: Fri May 13, 2005 12:09 pm
- Location: Ootmarsum, Netherlands
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
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.
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.
-
- Newbie
- Posts: 2
- Joined: Fri May 13, 2005 12:09 pm
- Location: Ootmarsum, Netherlands
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
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