Adding Outlook Tasks To A Calendar

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!!!

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David LeeCommented:
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?
bclivellAuthor Commented:
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!!
David LeeCommented:

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

bclivellAuthor Commented:

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!!
David LeeCommented:
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".
bclivellAuthor Commented:

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!

David LeeCommented:

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?  
bclivellAuthor Commented:

Thank you!!  I added the new questions here:

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.