Creating Outlook Tasks with PHP

Feb
06
2006

Last week I posted a script I'm using to make it easier to create a weekly milestone/status report. In my case, it's a specific client that wants these, but I know lots of employees who are required to do them as well.

Here's another piece in what is shaping up to be a string of tools for managing this kind of thing. This creates an Outlook task for taking daily notes on what happened each day. It will be run each Monday to create a task with a due date of Friday. The subject will contain the dates for the Sun-Sat current week.

It also serves as a fairly simple example of creating tasks in Outlook automatically. The code is commented to explain what's going on. As with the previous solution, I'm using the Windows task scheduler to have this kick off on Monday morning. I'll likely write something up to easily "self-schedule" as well as I am already irritated at having to manually set these up even the once. (I know, low tolerance for irritability).

< ?php
/* Define Outlook number for Task Item */
$olTaskItem = 3;

/* Create an instance of Outlook */
$ol = new COM("Outlook.Application");

/* Create a Task Item */
$task = $ol->CreateItem($olTaskItem);

/*
Set dates for current week.
PHP's strtotime needs the chosen strings to give us the Sun-Sat week.
If you choose to use a Mon-Fri week, the "last" becomes "this" for the beginning.
It's just how PHP and strtotime work.
*/
$begin = date("m/d/y", strtotime("last Sunday"));
$end = date("m/d/y", strtotime("this Saturday"));
$due = date("m/d/y", strtotime("this Friday"));

/* Set some properties of the task */
$task->Subject = "Milestone Notes for $begin to $end";
$task->DueDate = $due;
$task->Body = "Insert the template text for your task";

/* Save the Task */
$task->Save();

print("Done");
?>

If you're curious about modifying the task a little more, you can see the complete list of properties (Subject, Body, DueDate, etc.) and Methods (like Save) in the Outlook documentation for the TaskItem. Most of the examples are in VBScript, but that's generally the case with Office documentation. However, use my sample script as your guide and you can pretty quickly see the syntax conversion you need to do to make their examples work (is there interest in a post showing how to more generally convert VBScript or Javascript into PHP?).

 

Comments on this post

Feedback is always welcome. Read some from other folks or leave your own below. Just keep things civil and remember that what you post lives on in public. Forever.

Thanks,
J

15 Responses to “Creating Outlook Tasks with PHP”

  1. Chase Says:

    I am in IT for small company and our internal software is all done in PHP. I know PHP very well, but my knowledge of COM stuff is very weak. Our telephone system (Televantage) has a great API for doing all sorts of stuff, but its all COM based. There were a few sample scripts (VB i believe) but was having trouble implementing them in ASP (which I dont know well at all) and I really am struggling with trying to take those scripts and convert them for use in PHP with the COM object. So any help you could give would be great. Also, is it possible to create a task in outlook and assign it to someone else? We want our managers to be able to mark an item for an employee, and have it auto generate an outlook task for that employee.

    Thanks for your help

  2. J Wynia Says:

    I did start on my "porting ASP to PHP" article, and I'll work on finishing it up when I get a chance. If you can send me a link to their documentation, I might be able to use it as an example in my posting using their objects.

    The biggest syntax change is that PHP uses $object->property syntax while VBScript uses object.property. Also, whenever you see a constant like

    myOlApp.CreateItem(olTaskItem)

    the "olTaskItem" is a constant that you'll have to look up. The constant is often just "there" if you're doing it in VBA, but in PHP, you'll need the actual value of "3" instead.

    In Outlook, you can defintely assign tasks. Effectively, you can do pretty much anything with COM in Outlook that you can do manually. That's really the power of the automation stuff. In the above code, once you reach the point where you're setting properties:

    $task->Subject = "Subject";

    The variable $task is what the Outlook object model calls an "Item". It's specifically a TaskItem. If you look in the Outlook documentation at the object "TaskItem", you will see all of the properties and methods you can use on a task.

    The Outlook object model is described here:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_2003_ta/html/odc_landoffice03_vba.asp

    TaskItems are described here:
    http://msdn.microsoft.com/library/en-us/vbaol11/html/olobjTaskItem_HV05247372.asp?frame=true

    Down at the bottom of that documentation page is a huge list of those properties an methods. "Save()" is one of those that I used in my sample. For your project, you'll want to look at "Assign" to send them out and possibly "Mark Complete" and "Delete" if you want to cancel from the same place you're assigning.

    Take a look at the VB examples and compare them with my PHP above and you should see the patterns you'll need to convert all of the example code into PHP.

    Oh. You may also need to install a program that I don't have the name/URL of handy that overrides Outlook's tight security to enable your app to really dig into Outlook.

  3. leonardo Says:

    How I get the number for OlForderContacts? because I got this error when I tried to use other folder.

    Warning: (null)(): Invoke() failed: Type mismatch. Argument: 2

  4. J Wynia Says:

    All of the outlook constants are defined in the documentation I linked to.

  5. John Says:

    Hi!

    i have tested this code but i think there is something wrong. i have outlook 2003, win xp, and php 4, is there any issue with outlook 2003 and the foloing code?

    sorry about my english (i'm portugese)

    thanks ;)

  6. Peter Winch Says:

    Hi,

    Need to know where I can find list of valid commands, ie, Worksheet->Add(); for excel COM objects.

    Can you help?

  7. J Wynia Says:

    Just go up the tree from the documentation I linked to. All of the Office objects are there.

  8. seo Says:

    this code seems to working for me. cheers!

  9. Problem :Create an instance of Outlook Says:

    I am trying only the code

    But getting the error as :

    Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `Outlook.Application.11': Server execution failed ' in C:\Program Files\Apache Group\Apache2\htdocs\powerplate\test4.php:2 Stack trace: #0 C:\Program Files\Apache Group\Apache2\htdocs\powerplate\test4.php(2): com->com('Outlook.Applica…') #1 {main} thrown in C:\Program Files\Apache Group\Apache2\htdocs\powerplate\test4.php on line 2

    I have isntalled Windows XP / MSOutlook 2003 /Apache 2.054 /PHP 5.0.3

    Please help

  10. egon muencberger Says:

    com functions are NOT available under a linux distribution - windows only, you will have to resort to an activeXObject instead!

  11. Pascal Says:

    hi there!

    as i want to develop a webbased tool in PHP4, which sends tasks and meetings to my outlook as an email (as if somebody else would send me the task from his outlook), i wonder, how the task can be sent.

    i already managed to send me meetings with the vCalendar standard used in Outlook:
    BEGIN: VEVENT

    END: VEVENT

    As in Outlook 2003 the VTODO does not exist, I don't know how to continue.

    Can anybody help me? Do I need to install someting particular on the webserver that I can make use of the Outlook Object with PHP?

    Regards
    Pascal

  12. ksign Says:

    very helpfull !!
    I'm working on it, I might publish some more examples when I handle.

  13. Elaine Says:

    Hi, please help on this,

    Unable to connect to Outlook: Failed to create COM object `Outlook.Application': Call was rejected by callee.

    Would appreciate very much for helping. Thanks

  14. Toby Says:

    This is great but can one do something fairly easy like loop through the tasks already present in Outlook and display them as a list on a webpage?

  15. Sly Says:

    Hi,

    This script seem don't word with outlook 2003 / 2007.

    Someone can help me ?

    Thanks.

Leave Your Own Comment

By submitting a comment, you agree to license it under the terms of the Creative Commons Attribution license.

People who post comments get the added benefit of visiting the site without advertising.

© 2003-2009 J Wynia. All original content is licensed under the terms of the Creative Commons Attribution license unless otherwise noted. Content from other sources is licensed under its original terms.