Adding Outlook Tasks To A Calendar

Posted on 2008-02-06
Medium Priority
Last Modified: 2010-09-29
Hi Everyone-

I have a question for with regards to Outlook and possibly some VBA programming.  I am using the tasks feature of Outlook.  With outlook, you have your main task folder and then you can also create some sub folders tasks taht are seperate from your main task folder.  I would like to see the individual tasks i enter appear on the calendar as multi day events based on the start and end dates so I do not have to enter the information twice (first add it to my task list and then add it to a calendar.  More speficially, I would like to:

Sub Task folder: "Project Integrity - General"

1) Press a button that will create a new calendar in my Outlook based on the name of the task folder(in my example: "Project Integrity - General"
2) the code would then take the individual tasks and add them to the newly created folder as multiday events based on the start and end dates.  
3) If the calendar already exists, then it will just update the dates based on the dates in the task folder

A couple of things I need to have the code keep in mind:
        -I do not want it to update all of the tasks folders at once.  Ideally, it would be create if a message box could appear and I could select the task folder to update so I can update them one at a time.  If not, I just want to ensure that nothing appears in my main calendar.  
        -For tasks that I check off as completed, I want them to stay in the calendar, rather than being removed.  

Thank you so much for taking the time to help me with this.  Please let me know if I need to provide more information or if I was not clear.  Thank you!!!

Question by:bclivell
  • 4
  • 4
LVL 76

Expert Comment

by:David Lee
ID: 20836706
Hello again, Brian.

This is all doable.  Will all the task folders be sub-folders under Tasks and will all the calendars be sub-folders under Calendar?

Author Comment

ID: 20836869
Hi BlueDevilFan-

How are you?  I was hoping I would be able ot find you on here again!!

"Will all the task folders be sub-folders under Tasks and will all the calendars be sub-folders under Calendar?" <--You are correct.  I think this is the best way for organizational purposes. I just select new folder and have it add it as a sub folder

Thank you for your help!!
LVL 76

Accepted Solution

David Lee earned 2000 total points
ID: 20933651

Deepest apologies for being so slow.  Here's the code for doing this.  Follow these instructions to use it.

1.  Start Outlook.
2.  Click Tools->Macro->Visual Basic Editor.
3.  If not already expanded, expand Modules and click on Module1.
4.  Copy the code below and paste it into the right-hand pane of the VB Editor.
5.  Edit the code as needed.  I placed comment lines where things need to change.
6.  Click the diskette icon on the toolbar to save the changes.
7.  Close the VB Editor.
8.  Select a Task folder and run the macro.  It will check to see if a corresponding calendar exists under your main calendar and create it if it doesn't find one.  It then erases all appointments from that calendar, if any exist.  Last of all it loops through all the tasks creating an appointment for each one.  The start and end dates of the appointment are set to match the start and due dates of the task.  As a bonus I added code that creates a link back to the original task.  

This solution will only work in Outlook 2007.
Sub Task2Calendar()
    Dim olkTaskFolder As Outlook.MAPIFolder, _
        olkCalFolder As Outlook.MAPIFolder, _
        olkTask As Outlook.TaskItem, _
        olkAppt As Outlook.AppointmentItem, _
        intIndex As Integer
    On Error Resume Next
    Set olkTaskFolder = Application.ActiveExplorer.CurrentFolder
    Set olkCalFolder = Session.GetDefaultFolder(olFolderCalendar).Folders(olkTaskFolder.Name)
    'If the matching calendar doesn't exist, create it.
    If Err.Number <> 0 Then
        Set olkCalFolder = Session.GetDefaultFolder(olFolderCalendar).Folders.Add(olkTaskFolder.Name)
    End If
    'Wipe out all the calendar entries
    For intIndex = olkCalFolder.Items.Count To 1 Step -1
        olkCalFolder.Items.Remove (intIndex)
    For Each olkTask In olkTaskFolder.Items
        Set olkAppt = olkCalFolder.Items.Add()
        With olkAppt
            .Subject = olkTask.Subject
            .Start = olkTask.StartDate
            .End = olkTask.DueDate
            .Body = "Open Associated Task"
            InsertLink olkAppt, "outlook:" & olkTask.EntryID, "Open Associated Task"
        End With
    Set olkCalFolder = Nothing
    Set olkTaskFolder = Nothing
    Set olkTask = Nothing
    Set olkAppt = Nothing
    MsgBox "All done.", vbInformation + vbOKOnly, "Task2Calendar Macro"
End Sub
Sub InsertLink(msg As Outlook.AppointmentItem, strLink As String, strLinkText As String)
    'This is a modified version of Sue Mosher's example from
    Dim objInsp As Outlook.Inspector, _
        objDoc As Word.Document, _
        objSel As Word.Selection
    Set objInsp = msg.GetInspector
    Set objDoc = objInsp.WordEditor
    objDoc.Windows(1).Document.Range(0, 20).Select
    Set objSel = objDoc.Windows(1).Selection
    objDoc.Hyperlinks.Add objSel.Range, strLink, "", "", strLinkText, ""
    If Err.Number <> 0 Then
        MsgBox "Problem adding hyperlink to appointment." & vbCrLf & Err.Number & ": " & Err.Description, vbCritical + vbOKOnly, "Task2Calendar"
    End If
    Set objInsp = Nothing
    Set objDoc = Nothing
    Set objSel = Nothing
End Sub

Open in new window

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.


Author Comment

ID: 20937384

This code works great!  One minor thing, for some reason the due date was ending a day early on all of the tasks so I added "+1" to the line ".End = olkTask.DueDate".

I do have a follow up question:
Is there a way to add tasks from different task folders to the same calendar folder?  For example,  I have a general project calendar (called "Project Calendar" where I have all my project tasks and I want to see the overlap.  The unique id so to speak would be based on combining the task subject with the task folder name.  For example: ".Subject = olkTask.Subject & " - " & olkTaskFolder.Name".  If the appoitment exists, then simply update it.  If the task does not exist, then add the appointment to the calendar.  Please let me know if I should make a seperate question or if this is okay to include with this one.  Also, please let me know if I need to explain further.  

Thank you!!
LVL 76

Expert Comment

by:David Lee
ID: 20943442
Hi, Brian.

I don't know why the date would be a day off.  That's a good fix for now.  Just remember that the appointments will be a day off again if you run this against a folder with tasks that aren't a day off to begin with.  

Yes, it is possible to do what you described in your follow-up question, but there's no need to.  Use calendar overlays to achive the same effect without the need to do any of this.  Outlook 2007 introduces the ability to stack calendars on top of each other so you can see their details together.  For more details open Outlook help and search for "View calendars side-by-side or overlaid".

Author Comment

ID: 20943947

Thank you for the note about the days potential being off.  I had not thought about that.  

In regards to the overlays, I actually was trying to do that, but outloook wont let me print in an overlay view.  Also, it will not show the color coding i have put in place for the catagories.  Do you know a way around this issue other than what I was trying to do?  If not, when you have some time, would you be able to assist me with the code?

Thank you and have a wonderful night!

LVL 76

Expert Comment

by:David Lee
ID: 20956700

Ok, we can go the code route.  Can we do it as a separate question though since it's something that's outside the scope of this original question?  

Author Comment

ID: 20957304

Thank you!!  I added the new questions here: http://www.experts-exchange.com/Software/Office_Productivity/Groupware/Outlook/Q_23184421.html


Featured Post

Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There can be many situations demanding the conversion of Outlook OST files to PST format and as such, there is no shortage of automated tools to perform this conversion. However, what makes Stellar OST to PST converter stand above the rest? Let us e…
MS Outlook undoubtedly is the most widely used email client.Its user-friendliness, cost effectiveness, and availability with Microsoft Office Suite make it the most popular email application.  Its compatibility with Microsoft applications like Exch…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question