Hook the completion event

Kenny Hopton
Kenny Hopton used Ask the Experts™
on
I am new to Outlook coding and am really only doing VBA at this point.  I want to have a script that runs whenever a Task item is marked complete.  The idea is to manipulate the item and then set it incomplete again.

I expect to derive a new class such as Item.Task.Regenerating from Task and tweak the forms a bit but I don't know how many places I need to hook to get all possible events that toggle a task to be completed.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010

Commented:
Hi, khopton.

There is no completion event in Outlook.  You can trap events for the task folder and check items as they are saved taking action if the task is marked complete.  VBA does not support object inheritance so you cannot derive a class.  You can write your own class, but you cannot inherit from any existing Outlook objects.

Author

Commented:
Thanks for responding.  

- Can you point me to an example of trapping events for the task folder?  I assume that when a user changes the complete icon in the folder view this event fires, right?

- I guess when a user opens an item, changes the completed status and saves it I can get in before the save completes and change the item.

Do these two approaches encompass all the ways a task gets completed?  What I'm really asking here is - can I assume that no task will have it's completed status changed without me knowing if I monitor these two things?

Thanks for your help!
Top Expert 2010
Commented:
Apologies for taking so long to respond to you.  The code below will trap all task additions and modifications without regard to how they are added or changed.  The code must go in the ThisOutlookSession module in Outlook.
Dim WithEvents olkTasks As Outlook.Items 
Private Sub Application_Quit()
    Set olkTasks = Nothing
End Sub 
Private Sub Application_Startup()
    Set olkTasks = Outlook.Session.GetDefaultFolder(olFolderTasks).Items
End Sub 
Private Sub olkTasks_ItemAdd(ByVal Item As Object)
    CompletedTask Item
End Sub 
Private Sub olkTasks_ItemChange(ByVal Item As Object)
    CompletedTask Item
End Sub 
Sub CompletedTask(olkTask As Outlook.TaskItem)
    'Your code goes here'
End Sub

Open in new window

Author

Commented:
Just what I needed.  Thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial