Outlook calendar export

It should be quite easy to export Outlook appointments into .ics format and let third party applications like phpicalendar stitch these snippets together into a coherent view. Unfortunately older Outlook versions are not supporting ics export. After several frustrating attempts (installing VB macros that are buggy and software like freemical that misses important fields) I finally wrote my own exporter that includes also a category filter as otherwise too may entries will have to be parsed by phpicalendar.

ol2ics.zip includes the source perl script including a compiled windows version.

 1:
 2:
 3:
 4:
 5:
 6:
 7:
 8:
 9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
62:
# ol2ics.pl outlook to ics export
# m@wjst.de 26Dec07
# ------------------------------------------------------------
#!/usr/local/bin/perl

if ($ARGV[0] eq "") {
    print ("OL2ICS export.ics categoryname\n");
    exit(1);
}

open(OUT,">$ARGV[0]");
print OUT ("BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//RSB//ICSCATEG//EN\n");
$b="BEGIN:VEVENT\n";
$e="END:VEVENT\n";

use Win32::OLE;
use Win32::OLE::Const 'Microsoft Outlook';

my $outlook = Win32::OLE->new('Outlook.Application') or die "Error!\n";
my $namespace = $outlook->GetNamespace("MAPI") or die "can't open MAPI namespace\n";;
my $folder = $namespace->GetDefaultFolder(olFolderCalendar);

my $items = $folder->Items;
for my $itemIndex (1..$items->Count) {
    my $message = $items->item($itemIndex);
    next if not defined $message;

    $s="SUMMARY:" . $message->{Subject} . "\n";
    # $message->{Body}
    # $message->{IsRecurring}

    $l="LOCATION:" . $message->{Location} . "\n";
    if ($message->{AllDayEvent} eq 1) {
        $t="DTSTART;VALUE=DATE:" . $message->{Start}->Date("yyyyMMdd") . "\n";
        $d="DTEND;VALUE=DATE:" . $message->{End}->Date("yyyyMMdd") . "\n";
    } 
    else {
        $t="DTSTART:" . $message->{Start}->Date("yyyyMMdd") . "T" . $message->{Start}->Time("hhmmss") . "Z\n";
        $d="DTEND:" . $message->{End}->Date("yyyyMMdd") . "T" . $message->{End}->Time("hhmmss") . "Z\n";
    }
    $c="CATEGORIES:" . $message->{Categories} . "\n";
    if ($message->{item.Sensitivity} eq 2) {
        $p="CLASS:PRIVATE\n";
    }
    elsif ($message->{item.Sensitivity} eq 1) {
        $p="CLASS:CONFIDENTIAL\n";
    }
    else {
        $p="CLASS:PUBLIC\n";
    }
    $m="DTSTAMP:" . $message->{LastModificationTime}->Date("yyyyMMdd") . "T" . $message->{LastModificationTime}->Time("hhmmss") . "Z\n";
    $u="UID:" . $message->{EntryID} . "\n";

    if ( $c =~ /.*($ARGV[1]).*/) {
        print OUT ("$b$s$l$t$d$c$p$m$u$e");
    }
}

print OUT ("END:VCALENDAR");
close OUT;
exit(0);

The ics file may then be uploaded to the phpicalendar directory using a wput call like
wput –reupload my.ics ftp://name:password@domain.de/calendar/my.ics