Tuesday, August 31, 2004

Creating a new folder in a mailbox if one doesn't exist

I've had this question from a few people that wanted to do this in a event sink, its acually really simple to do with a little bit of ADO/Exoledb code.

To create a folder in a mailbox using ADO you can use the adCreateCollection option in your record open statement. The following two lines would create a folder in a mailbox.

set rec = createobject("ADODB.Record")
rec.open "file://./backofficestorage/domain.com/MBX/mailbox/newfolder/", ,3,8192

The options included in the open statement are 3 which is adreadwrite and 8192 which is adCreateCollection (hex 0x2000)

If you try this code and the folder already exists you will get an error returned stating that the object already exists. So if you want to run the above code to create the folder only if the folder doesn't exist and not return an error if it does all you need to do is combine adCreateCollection with adOpenIfExists. eg

set rec = createobject("ADODB.Record")
rec.open "file://./backofficestorage/domain.com/MBX/mailbox/newfolder/", ,3,33562624

33562624 is the sum of adCreateCollection (hex 0x2000) and adOpenIfExists (hex 0x2000000)