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

I am attempting desperately (and failing) to use Win32::OLE to manipulate emails in Outlook 2007. The code below is a fairly stripped down version... note that I have no problem marking an email Read or Unread, however setting other properties, and calling some methods, doesn't work. :( The most frustrating part is that once in awhile, it DOES work for 1 email, but not any of the others! And I can't reliably reproduce the success. In desperation, you'll note in the code that I tried looping through a single email infinitely until it set the Category correctly... but that doesn't work either :( Please show me the error(s) of my ways! Thanks for any response, even if it is just to call me stupid!
use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Outlook'; $|++; #get an Outlook object my $outlook; $outlook = Win32::OLE->new('Outlook.Application'); die unless $outlook; #get the Inbox folder my $namespace = $outlook->GetNamespace("MAPI"); my $folder = $namespace->Folders("My Folder"); my $n = $folder->Items->Count; my $i=0; my $from; for ($i=$n; $i>0; $i--) { $from = $folder->Items($i)->{'SenderEmailAddress'}; if ($from eq "sender@myemail.com") { if ($folder->Items($i)->{'Unread'}) { $folder->Items($i)->{'Unread'}=0; + #<--- this works print "Is it? before - ".$folder->Items($i)->{'IsMarkedAsT +ask'}."\n"; #<--- always returns 0 $folder->Items($i)->MarkAsTask(0); + #<--- I don't think this works print "Is it? after - ".$folder->Items($i)->{'IsMarkedAsT +ask'}."\n"; #<--- always returns 0 print "Cats before: ".$folder->Items($i)->Categories."\n"; while ($folder->Items($i)->Categories ne "NAC12182") { $folder->Items($i)->{'Categories'}="NAC12182"; + #<--- this doesn't work } print "Cats after: ".$folder->Items($i)->Categories."\n"; } } }

Replies are listed 'Best First'.
Re: Using Win32::OLE to set categories in Outlook 2007
by bart (Canon) on Apr 29, 2009 at 18:36 UTC
    You're mixing "IsMarkedAsTask", and "MarkAsTask". Perhaps you should use just one?
      I will try it, but from the MSDN doc: http://msdn.microsoft.com/en-us/library/bb176580.aspx MailItem.IsMarkedAsTask Property Returns a Boolean value that indicates whether the MailItem is marked as a task. Read-only.
      IsMarkedAsTask just returns a boolean so I can see if MarkAsTask worked or not... if I take it out, MarkAsTask still doesn't work...
Re: Using Win32::OLE to set categories in Outlook 2007
by jdavidson (Initiate) on May 01, 2009 at 05:21 UTC

    Hooray, I figured it out!
    Hopefully this helps someone else out there one day:

    I had to first:

    my $thisItem;  #<-- this is imperative!

    then:

    $thisItem = $folder->Items($i); if ($thisItem->{'Unread'}) { ...etc...

    and replace every instance of

    $folder->Items($i)

    with

    $thisItem

    Then it works like a charm!!
    Also don't forget to save changes at the end of changing each email:

    $thisItem->Save();

    Yay!

      Do you know how to send emails with Office 2007? Since upgrading I can't seem to figure it out. To create new messages I had the following code:
      $msg = $session->Outbox->Messages->Add();
      And now I get the following error message:"Can't call method "Messages" on an undefined value at"