nanojack has asked for the wisdom of the Perl Monks concerning the following question:

I have been reading about adding to outlook calednar from perl. I am not able to do this. I am able to read from outlook, but cannot write to the calendar, any on know how to do this???? Here is my code to read from outlook calendar
use Win32::OLE; use Win32::OLE::Const 'Microsoft Outlook'; use Digest::MD5 qw(md5_hex); use DBI; # Making a connection to a database $dbfile = 'U:\\scripts\\Perl\\outlook\\calendar.db'; $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", "", "", { RaiseError => 1, AutoCommit => 0 }); # Connection to Outlook Calendar my $outlook = Win32::OLE->new('Outlook.Application') or die "Error!\n"; my $namespace = $outlook->GetNamespace("MAPI"); my $folder = $namespace->GetDefaultFolder(olFolderCalendar); my $CalendarFolderItems = $folder->Items; print length($folder) . "\n"; for my $itemIndex (1..$CalendarFolderItems->Count) { my $CalendarItem = $CalendarFolderItems->item($itemIndex); next if not defined $CalendarItem; $start_date = $CalendarItem->{Start}->Date; $start_time = $CalendarItem->{Start}->Time; $end_date = $CalendarItem->{End}->Date; $end_time = $CalendarItem->{End}->Time; $duration = $CalendarItem->{Duration}; $subject = $CalendarItem->{Subject}; $categories = $CalendarItem->{Categories}; $body = $CalendarItem->{Body}; $reminder = $CalendarItem->{ReminderMinutesBeforeSta +rt}; # Setting up the MD5 to create a unique identifier $md5setup = $start_date . $start_time . $end_date . $e +nd_time . $subject . $body . $duration . $reminder; $md5hash = md5_hex($md5setup); if ($categories =~ m/Holiday/){ }else{ # searching and replaicing apostraphies $subject =~ s/\'/\''/g; $body =~ s/\'/\''/g; print "*****************************************\n"; print "Start Date " . $start_date . "\n"; print "Start Time " . $start_time . "\n"; print "End Date " . $end_date . "\n"; print "End Time " . $end_time . "\n"; print "Duration " . $duration . "\n"; print "Subject " . $subject . "\n"; print "Categories " . $categories . "\n"; print "Body " . $body . "\n"; print "Reminder " . $reminder . "\n"; print "Hash " . $md5hash . "\n"; # prepairing the sql statement $sql = "NULL,'$start_date','$start_time','$end_date','$end +_time','$duration','$subject','$categories','$body','$reminder','$md5 +hash'"; # $sql = $reminder . "," . $body . "," . $categories. "," +. $subject. "," . $duration . "," . $end_time . "," . $end_date . "," + . $start_time . "," . $start_date . "," . "," . $hash; print $sql . "\n"; $dbh->do("INSERT INTO calendar VALUES ($sql)"); $dbh->commit( ); } }

Replies are listed 'Best First'.
Re: Outlook Write to Calendar
by blm (Hermit) on Dec 20, 2005 at 03:50 UTC
    Adding appointment to outlook
    #!c:/perl/bin/perl.exe -w use strict; use Win32::OLE; use Win32::OLE::Const 'Microsoft Outlook'; # Connection to Outlook Calendar my $outlook = Win32::OLE->new('Outlook.Application') or die "Error!\n"; my $newappt = $outlook->CreateItem(olAppointmentItem); $newappt->{Subject} = "My Test Appointment"; $newappt->{Start} = "12/21/05 3:50:00 PM"; $newappt->{Duration} = 1 ; #1 minute $newappt->{Location} = "Someplace"; $newappt->{Body} = "Test Stuff"; $newappt->{ReminderMinutesBeforeStart} = 1; $newappt->{BusyStatus} = olFree; $newappt->Save();

    I wonder if people would be interested in a tutorial on translating vb/vbscript to perl and Win32::OLE. Then we can just point them to such a tutorial and the zillion vbscript/vb samples on the Internet. hmmm...

      Thank you for the informaiton. I think that a tutorial on translating vb/vbscript to perl and Win32::OLE would be a greate idea.
      -How important does a person have to be before they are considered assassinated instead of just murdered?
      Thanks alot guys this code works just fine....
Re: Outlook Write to Calendar
by Errto (Vicar) on Dec 19, 2005 at 20:55 UTC
    It looks like you're already pretty clear about how to call the relevant COM/OLE APIs from Perl, so really this is more of an Outlook API question than a Perl question. Your best bet is probably to go straight to the source in the source in the MSDN Library and look under the objects/methods for Outlook. Specifically it looks like you would use the Add method on the Items collection for your folder passing in the appropriate item type (probably olAppointmentItem).
      OK here is what I have, I am getting into some troubles, the script is failing at the last line. I am getting the error "Can't call method "Items" on an undefined value at Untitled11.pl line 9." Not sure what to do here,
      Thanks for all the help
      nanojack
      use Win32::OLE; use Win32::OLE::Const 'Microsoft Outlook'; # Connection to Outlook Calendar my $outlook = Win32::OLE->new('Outlook.Application') or die "Error!\n"; my $namespace = $outlook->GetNamespace("MAPI"); my $app = $namespace->CreateItem(olAppointmentItem); my $CalendarFolderItems = $app->Items;
      -How important does a person have to be before they are considered assassinated instead of just murdered?
        I read this code sample and interpret as following. You are trying to retrieve the collection of appointments in outlook. However what you seem to be doing is creating a single appointment in
        my $app = $namespace->CreateItem(olAppointmentItem);

        and trying to retrieve the Items collection of the Appointment. Perl is telling you that there is no Items collection attached to an Appointment Item.

        Look at your first question and try something allong the lines of:

        1. Retrieve the Namespace
        2. Get the DefaultFolder that contains Calendar objects.
        3. Retrieve collection of appointments
        4. With the collcation add an appointment.