Skip to main content

Adding Categories to the Master categories list in Outlook 2007 with a CDO 1.2 script

This is a two part post that I thought I’d separate this idea came from someone who asked about how you could group messages by their attachment types. Normally this would be a pretty hard thing to achieve manly because of the way attachments are stored doesn’t lend itself well to being grouped by in a search folder or an Outlook Customize view. But this got me thinking about what if you could use the new colored category feature in Outlook 2007 instead. Eg for each attachment type you have a separate color and Label. This works out pretty cool because you go from being able to look at your email and seeing that there is an attachment eg the paper clip icon to being able to look at a message and if you see a blue category mark you know that message has got an attachment and it’s a word document if it’s a green mark you know it’s a Excel document. You can then also create Views or Search folders based on the attachment categorization you could also could create an event sink or Notification app to assign the category to new items when they arrive.

To start with this idea however I first needed to make changes to the master categories list in Outlook 2007 while doing this from Outlook 2007 is the easy method I wanted to do this programmatically instead. The master categories list is held in a hidden message (in the associated contents collection) with a message class of “IPM.Configuration.CategoryList”. On this message there is a binary Mapi property 0x7C080102 which holds the category list which in is XML format. So to modify the list you need some code that will first read this property I used CDO 1.2 so when you read the property with CDO you get back a hex string representation of the Binary property. To make use of this the Hex needs to be converted to a String which will represent the XML document. I then loaded the XML back into the XMLDom object and used the clonenode method to copy one of the existing nodes and then modified the necessary properties for the new category I wanted to add. The three important bits of information you need to set are the Name which is the keyword value you going to use for you category. An integer for the color you want the category to be and a unique GUID value. To stop duplicates there’s some code to check if the name of Guid already exists in the XML document if so it doesn’t try to add another node. To write the modified XML back to the property theres a function that coverts the XML String back to hex.

The code will only work to modify an existing category list it won’t create one from scratch. To run the code you need to supply the servername and mailbox name of the mailbox you want to run it against as commandline parameters eg

Cscript modcats.vbs servername mailboxname

I’ve put a download of the scrip here the script itself looks like

snServername = wscript.arguments(0)
mbMailboxName = wscript.arguments(1)

' Word Documents
ReDim wdattah(1)
wdattah(0) = "{DB13F464-2FAA-48F2-8D1B-ADB5ED4FD1F7}"
wdattah(1) = 22
'Excel Attachments
ReDim edattach(1)
edattach(0) = "{D549D2BB-E1BF-47DE-B713-784771F059A1}"
edattach(1) = 19
'PowerPoint Attachments
ReDim pptattach(1)
pptattach(0) = "{1E8ADCFB-AC2C-4FEF-ABB5-C5349A359CC8}"
pptattach(1) = 0
'PDF attachments
ReDim pdfattach(1)
pdfattach(0) = "{707D20D7-5EF8-47D7-B6C8-47FCB606EEB5}"
pdfattach(1) = 15
'Audio Attachments
ReDim sndattach(1)
sndattach(0) = "{B28E76F5-127B-4356-9150-D2A0B84E8DCE}"
sndattach(1) = 18
ReDim vdoattach(1)
vdoattach(0) = "{E633EC9C-9B29-4608-A4BA-CFBFA886702B}"
vdoattach(1) = 23
'Image Attachment
ReDim imgAttach(1)
imgAttach(0) = "{BB488D85-76FE-408F-9DD4-617041DBFDA6}"
imgAttach(1) = 13
'Zip Attachment
ReDim zipAttach(1)
zipAttach(0) = "{B4423425-54F1-304F-92F3-63451D3BFDB6}"
zipAttach(1) = 8

Set catDict = CreateObject("Scripting.Dictionary")
catDict.add "Word Attachment",wdattah
catDict.add "Excel Attachment",edattach
catDict.add "PowerPoint Attachment", pptattach
catDict.add "PDF Attachment", pdfattach
catDict.add "Audio Attachment", sndattach
catDict.add "Image Attachment", imgAttach
catDict.add "Video Attachment", vdoattach
catDict.add "Zip Attachment", zipAttach

set xdXmlDocument = CreateObject("Microsoft.XMLDOM")
Set objSession = CreateObject("MAPI.Session")
objSession.Logon "","",false,true,true,true,snServername & vbLF & mbMailboxName
Set CdoInfoStore = objSession.GetInfoStore
Set CdoFolderRoot = CdoInfoStore.RootFolder
set cdocalendar = objSession.GetDefaultFolder(CdoDefaultFolderCalendar)
For Each soStorageItem in cdocalendar.HiddenMessages
If soStorageItem.Type = "IPM.Configuration.CategoryList" Then
hexString = soStorageItem.fields(&h7C080102).Value
For Each cat In catDict
catval = catDict(cat)
If SearchforCategory("name",cat,xdXmlDocument) = True Or SearchforCategory("guid",catval(0),xdXmlDocument) Then
wscript.echo "Category Name or GUID alread Exists " & cat
wscript.echo "Adding category " & cat
Call AddCategory(cat,catDict(cat),xdXmlDocument)
End if
nval = StrToHexStr(CStr(xdXmlDocument.xml))
soStorageItem.fields(&h7C080102).Value = nval
End if

Function hextotext(binprop)
arrnum = len(binprop)/2
redim aout(arrnum)
slen = 1
for i = 1 to arrnum
if CLng("&H" & mid(binprop,slen,2)) <> 0 then
aOut(i) = chr(CLng("&H" & mid(binprop,slen,2)))
end if
slen = slen+2
hextotext = join(aOUt,"")
end Function

Function StrToHexStr(strText)
Dim i, strTemp
For i = 1 To Len(strText)
strTemp = strTemp & Right("0" & Hex(Asc(Mid(strText, i, 1))), 2)
StrToHexStr = Trim(strTemp)
End Function

Function SearchforCategory(elElementName,cnvalue,XMLDoc)
Set xnNodes = XMLDoc.selectNodes("//*[@" & elElementName &amp; " = '" & cnvalue & "']")
If xnNodes.length = 0 Then
SearchforCategory = False
SearchforCategory = True
End if

End Function

Function AddCategory(cnCategoryName,setarray,XMLDoc)
Set xnNodes = XMLDoc.selectNodes("//categories")
Set xnCatNodes = XMLDoc.selectNodes("//category")
Set objnewCat = xnCatNodes(0).cloneNode(true)
objnewCat.setAttribute "name",cnCategoryName
objnewCat.setAttribute "guid",setarray(0)
objnewCat.setAttribute "keyboardShortcut", 0
objnewCat.setAttribute "color", setarray(1)
objnewCat.setAttribute "usageCount", 0
objnewCat.setAttribute "lastTimeUsedNotes","1601-01-01T00:00:00.000"
objnewCat.setAttribute "lastTimeUsedJournal","1601-01-01T00:00:00.000"
objnewCat.setAttribute "lastTimeUsedTasks","1601-01-01T00:00:00.000"
objnewCat.setAttribute "lastTimeUsedContacts","1601-01-01T00:00:00.000"
objnewCat.setAttribute "lastTimeUsedMail","1601-01-01T00:00:00.000"
objnewCat.setAttribute "lastSessionUsed","1601-01-01T00:00:00.000"
xnNodes(0).appendChild objnewCat
End Function

Popular posts from this blog

Downloading a shared file from Onedrive for business using Powershell

I thought I'd quickly share this script I came up with to download a file that was shared using One Drive for Business (which is SharePoint under the covers) with Powershell. The following script takes a OneDrive for business URL which would look like This script is pretty simple it uses the SharePoint CSOM (Client side object Model) which it loads in the first line. It uses the URI object to separate the host and relative URL which the CSOM requires and also the SharePointOnlineCredentials object to handle the Office365 SharePoint online authentication. The following script is a function that take the OneDrive URL, Credentials for Office365 and path you want to download the file to and downloads the file. eg to run the script you would use something like ./spdownload.ps1 '

Export calendar Items to a CSV file using EWS and Powershell

Somebody asked about this last week and while I have a lot of EWS scripts that do access the Calendar I didn't have a simple example that just exported a list of the Calendar events with relevant information to a CSV file so here it is. I've talked on this one before in this howto  but when you query the calendar folder using EWS you need to use a CalendarView which will expand any recurring appointments in a calendar. There are some limits when you use a calendarview in that you can only return a maximum of 2 years of appointments at a time and paging will limit the max number of items to 1000 per call. So if you have a calendar with a very large number of appointments you need to break your query into small date time blocks. In this example script I'm just grabbing the next 7 days of appointments if you want to query a longer period you need to adjust the following lines (keeping in mind what I just mentioned) #Define Date to Query $StartDate = (Get-Date) $EndDate

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why I wrote th
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.