Jul 132012

Recently, I wanted to make a script to read the subjects from a set of emails that I was receiving that was telling me what jobs I’d been assigned in the internal ticketing system at work.
Each time I got assigned a job, it would send me an email. Using this little scriptlet, I made a display to show me what the subjects were so I could see what jobs I had.

First step is to create the ActiveX object that taps into Outlook, and set that object up.

outlookApp = new ActiveXObject("Outlook.Application");
nameSpace = outlookApp.getNameSpace("MAPI");

Once the object has been setup, we can select the desired folder.
The getDefaultFolder(6) selects the Inbox as the folder start from.
The Inbox object then has all the folders under the Inbox assigned to it.

mailFolder = nameSpace.getDefaultFolder(6);
var Inbox = mailFolder.Folders;

Next up, I’m selecting the subfolder that I want the email subjects from, and assigning it to the box variable.

var box = Inbox.Item("SRs Assigned To Me").Items;

This bit is where we read the subjects from the emails themselves.
We get the first item in the folder using the GetFirst method, then we use a while loop to keep getting the subjects until there are no more emails. In this example, all the subjects are appended to the list variable, but you can choose to put them into an array, or echo them out directly using WScript.Echo. The choice is up to you.

var boxmail = box.GetFirst;
var list = '';
while(boxmail != null) {
list += boxmail.subject+"\n";
boxmail = box.GetNext;

Lastly, we tell the script to tell outlook to close all the connections that it opened to read the emails.

olLoggedOn = false;
nameSpace = null;
outlookApp = null;

With this script, you can view other properties of the emails, or count how many emails you have in a specific folder, or even in the Inbox itself. Putting this snippet into an HTA will even allow you to make a little email counter window.


 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>