Prevent an event loop when saving Task item in event handler

Prevent an event loop when saving Task item in event handler

Post by UGF1bFB » Thu, 25 Oct 2007 23:14:01


I'm using the event handler code below to modify the Subject of a Task
whenever I manually change its Due Date. The Save method in the code
triggers another ItemChange event. I use a boolean public variable to avoid
an event loop (examples in VBA help do the same). There is no way I know of
to disable events as there is in Excel VBA (with Application.EnableEvents =
False).

My problem is that this code ALSO runs whenever I Snooze a Reminder for that
Task. I don't care that the code is executed again. BUT, the boolean
variable behaves differently: With blnChangingTask = True (confirmed in the
Locals window) when the Save method triggers the event again blnChangingTask
somehow reverts back to False and so I get trapped in a an endless loop!!!
blnChangingTask is a module level variable, so I don't see why it does this?
When I hit Ctrl-Break to get out of the loop and then step throught the code
using F8 it behaves just as intended and exits the loop!!
Is this something to do with late binding perhaps??

I need to be able to EITHER:
1) Disable all events temporarily (as you can in Excel)
2) Prevent Snooze from firing my event handler code (I've tried the
ReminderFire event, but can get that to work yet either)
OR
3) Fix the weird issue with the blnChangingTask variable to block the loop.

Apologies for the long explanation. My thanks for any suggestions,
Paul


Public WithEvents colTasks As Outlook.Items 'object collection
Dim blnChangingTask As Boolean 'module level variable

Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.GetNamespace("MAPI")
Set colTasks = objNS.GetDefaultFolder(olFolderTasks).Items
blnUpdatingCompany = False
Set objNS = Nothing
End Sub

Private Sub colTasks_ItemChange(ByVal objItem As Object)
Dim objTask As TaskItem
Dim strSubject As String
'supposed to prevent code below from triggering an event loop:
If Not blnChangingTask Then
blnChangingTask = True
If objItem.Class = olTask Then
Set objTask = objItem
strSubject = objTask.Subject
'modify task subject:
strSubject = "XXX" '(subject is actually changed here based on the new
due date)
objTask.Subject = strSubject
objTask.Save '(this triggers another ItemChange event)
End If
End If
blnChangingTask = False
End If
Set objTask = Nothing
End Sub

Private Sub Application_Quit()
Set colTasks = Nothing
End Sub
 
 
 

Prevent an event loop when saving Task item in event handler

Post by Ken Slovak » Fri, 26 Oct 2007 06:15:35

Why not just test for the subject to see if it needs modification before
changing it? That would avoid endless loops into ItemChange.

--
Ken Slovak
[MVP - Outlook]
http://www.yqcomputer.com/
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.yqcomputer.com/

 
 
 

Prevent an event loop when saving Task item in event handler

Post by UGF1bFB » Sat, 27 Oct 2007 11:46:00

hanks Ken.
I actually do what you suggest. But it is the Reminder that causes the
problem (ItemChange event fired by clicking Snooze). The Subject is the same
whether I'm modifying the task due date or whether I'm clicking Snooze.

"Ken Slovak - [MVP - Outlook]" wrote:

 
 
 

Prevent an event loop when saving Task item in event handler

Post by Michael Ba » Sat, 27 Oct 2007 14:22:51


You could also track the Snooze event. If that's fired 'turn off' your
ItemChange handler.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Organize eMails:
<http://www.vboffice.net/product.html?id=2006063&cmd=detail&lang=en&pub=6>

Am Thu, 25 Oct 2007 19:46:00 -0700 schrieb PaulPW:

same
know
Application.EnableEvents
loop!!!
new
 
 
 

Prevent an event loop when saving Task item in event handler

Post by Ken Slovak » Wed, 07 Nov 2007 06:14:55

Clicking Snooze changes the FlagDueBy property so it would fire an
ItemChange event. Do what Michael suggested, set a flag to disable the
ItemChange code when the Snooze event fires.

--
Ken Slovak
[MVP - Outlook]
http://www.yqcomputer.com/
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.yqcomputer.com/