Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb thread: event handler of a dynamic set of objects


Message #1 by "Ryan Tyler" <ryan_tyler_2001@y...> on Wed, 16 Jan 2002 17:18:05
> I need to trap an event of a set of outlook objects.  I can easily trap 
> the event for one object by doing this.
> 
> '*********************
> Private WithEvents oItems        As Outlook.items
> 
> Set oFolder = objNS.GetDefaultFolder(olFolderInbox).Folders("Inbox")
> Set oCIQItems = oFolder.items
> 
> Private Sub oItems_ItemAdd(ByVal obj As Object)
>     MsgBox ("HI")
> End Sub
> 
> '*********************
> 
> But i will have a variable number of folders determined at run time.  I 
> need to trap the same event on this SET of folders.  Any Ideas?

Create a class, outlookEventsHandler:

Option Explicit
Private WithEvents mOutlookItems As Outlook.items

Public Property Set items(items As Outlook.items)
 Set mOutlookItems = items
End Property

Private Sub mOutlookItems_ItemAdd(ByVal obj As Object)
 Msgbox ("HI")
End Sub

- then, for each new folder that is created, create a new 
outlookEventsHandler - call it newHandler, say - and set newHandler.items 
= newFolder.items.

You can keep all your outlookEventsHandlers in a collection, either 
globally declared or private to whichever form or object will be keeping 
track of your Outlook folders. Provided a reference to the collection is 
maintained - that is, until it falls out of scope - all of the 
outlookEventsHandlers you create and set the items property for will pick 
up the events and handle them.

I do the same with forms on web pages, where I need to trap onSubmit 
events for forms in a customized browser. When the document loads, I 
create a formEventHandler for every form in the document, pass a reference 
to the form to the object, add the object to a collection declared as 
private at form/class module level and let it take care of the event 
handling.

  Return to Index