Export (0) Print
Expand All
Expand Minimize

Office Space: Tips and Tricks for Scripting Microsoft Office Applications

Office Space

Welcome to Office Space, the new column that offers tips and tricks for scripting Microsoft® Office applications. We’ll post new tips every Tuesday and Thursday; to see an archive of previous tips, visit the Office Space Archive. And if you have particular questions about Microsoft Office scripting, feel free to send them to scripter@microsoft.com (in English, if possible). We can’t promise to answer all the questions we receive, but we’ll do our best.

Finding the Number of Unread Messages in Your Inbox

Email: can’t live with it, can’t live without it. As the volume of email we get increases by leaps and bounds, people start looking for ways to automate and streamline their email use. Here’s a simple little script that might help: it checks your Outlook Inbox to find out how many unread messages you have:



Const olFolderInbox = 6

Set objOutlook = CreateObject("Outlook.Application")

Set objNamespace = objOutlook.GetNamespace("MAPI")
objNamespace.Logon "Default Outlook Profile", , FALSE, TRUE    
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)

Wscript.Echo objFolder.UnreadItemCount

objOutlook.Quit



One thing to be careful about when scripting against Microsoft Outlook is the fact that you can create only a single instance of Microsoft Outlook. If Outlook is already running, you won’t get a new instance when running this script; instead, you’ll bind to the existing instance. That’s not that big of a deal except when you reach the final line of code: the last line causes Outlook to quit, which means your existing Outlook session will terminate. For now, we’re going to assume that you’ll be running this script only when Outlook isn’t already running. We’ll deal with determining whether Outlook is already running and how to modify the script accordingly in a future column.

We start off by defining a constant named olFolderInbox and assigning it the value of 6; this constant will be used to specify the folder we want to open. (As you might expect, olFolderInbox represents the Inbox.) We then create an instance of Outlook and use the GetNamespace method to connect to the MAPI namespace. Note that while you must pass “MAPI” as the parameter to GetNamespace, that’s the only parameter you can pass; Outlook can’t connect to any other namespaces.

We then call the Logon method to ensure that we connect to the proper profile. The Logon method requires four parameters:

Parameter

Description

Profile

Outlook profile we want to connect to. This sample script uses “Default Outlook Profile”.

Password

Optional password to be used when connecting. This parameter exists primarily for backwards compatibility purposes, and will almost always be left blank.

ShowDialog

Specifies whether the profile selection dialog box should be shown.

NewSession

Specifies whether a new Outlook session should be created. In this script we are assuming that Outlook is not running; therefore, we need to create a new session.


After connecting to the correct profile we then use the GetDefaultFolder method to bind to the Inbox. (Note that we pass olFolderInbox as the sole parameter to GetDefaultFolder.) At that point we simply echo the value of the UnreadItemCount and then close Outlook.

By the way, you can determine the total number of items – both read and unread – that are in your Inbox by returning a collection of Inbox items and then echoing back the Count property for that collection. Here’s a sample script that does just that:



Const olFolderInbox = 6

Set objOutlook = CreateObject("Outlook.Application")

Set objNamespace = objOutlook.GetNamespace("MAPI")
objNamespace.Logon "Default Outlook Profile", , False, True    
   
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)

Set colItems = objFolder.Items
Wscript.Echo colItems.Count

objOutlook.Quit



Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft