Friday, November 11, 2011

Folders Collection Events


Folders Collection Events

The Folders collection object contains all the MAPIFolder objects belonging to a parent MAPIFolder object. The NameSpace object also contains a Folders object containing all the root folders for the currently logged-on user. The Folders collection object events occur when folders are added, changed, or deleted because of user action or program code. The Folders collection events give you a powerful means to control folder names, hierarchical structure, and folder contents, in addition to traditional Exchange roles and folder permissions.
With the proper synchronization of FolderAdd and FolderRemove events, you can actually restore deleted items and folders. Previously, this functionality was very difficult to achieve, requiring Exchange Server 5.5 scripting agents.

FolderAdd

The FolderAdd event occurs when a folder is added to a Folders collection object, through either user action or program code. The following example prevents users from deleting the Large Messages folder in their Inbox. If a user deletes this folder, a message box appears and notifies the user that the folder cannot be deleted. The folder is then moved from the user's Deleted Items folder back to the Inbox.
This example requires that you declare two collection object variables using WithEvents: colDeletedItemsFolders and colInboxFolders. When a folder is deleted from colInboxFolders, the FolderRemove event fires. The global variable blnDeleteRestore is set to True in the FolderRemove event procedure for colInboxFolders. When a folder is deleted from colInboxFolders, it is moved to the Deleted Items folder. The FolderAdd event procedure for thecolDeletedItemsFolders object evaluates the blnDeleteRestore variable. If blnDeleteRestore is True, the procedure moves the folder that has just been added to the Deleted Items folder back to the Inbox.
'Place these declarations in the general section of ThisOutlookSession
Public WithEvents colDeletedItemsFolders As Outlook.Folders
Public WithEvents colInboxFolders As Outlook.Folders
Public blnDeleteRestore As Boolean

Private Sub Application_Startup()
    Set objNS = Application.GetNameSpace("MAPI")
    Set colInboxFolders = objNS.GetDefaultFolder(olFolderInbox).Folders
    Set colDeletedItemsFolders = _
       objNS.GetDefaultFolder(olFolderDeletedItems).Folders
End Sub

Private Sub colInboxFolders_FolderRemove()
    blnDeleteRestore = True
End Sub

Private Sub colDeletedItemsFolders_FolderAdd(ByVal Folder As MAPIFolder)
    If blnDeleteRestore Then
        MsgBox "You cannot delete " & Folder.Name _
        & vbCr & "This folder will be restored to your Inbox.", _
            vbInformation
        Folder.MoveTo objNS.GetDefaultFolder(olFolderInbox)
    End If
    blnDeleteRestore = False
End Sub

FolderChange

The FolderChange event occurs when a folder in a Folders collection object is changed, either through user action or program code. The FolderChange event fires if a user or program code renames a folder, or if an item in the folder is added, changed, or removed. The following code example prevents a user from renaming the Large Messages subfolder of their Inbox. Outlook uses the Large Messages subfolder to store messages above a certain size limit that a user does not want to download to their offline folders file. The blnLargeMsgActive global variable ensures that the code runs only if the Large Messages folder is the current folder in the Explorer. All the necessary event procedures have been included in this example so that you can see how events have to be chained together to achieve the intended result.
'Place these declarations in the general section of ThisOutlookSession
Public WithEvents objExpl As Outlook.Explorer
Public WithEvents colInboxFolders As Outlook.Folders
Public blnLargeMsgActive As Boolean

'Create objExpl in the Application Startup event
Private Sub Application_Startup()
    Set objExpl = Application.ActiveExplorer
End Sub

'Create colInboxFolders in BeforeFolderSwitch and set blnLargeMsgActive
Private Sub objExpl_BeforeFolderSwitch(ByVal NewFolder As Object, _
  Cancel As Boolean)
    If NewFolder.Name = "Large Messages" And _
      NewFolder.Parent.Name = "Inbox" Then
        blnLargeMsgActive = True
    Else
        blnLargeMsgActive = False
    End If
    If NewFolder.Parent.Name = "Inbox" Then
        Set colInboxFolders = NewFolder.Parent.Folders
    End If
End Sub

Private Sub colInboxFolders_FolderChange(ByVal Folder As MAPIFolder)
    If blnLargeMsgActive Then
        If Folder.Name <> "Large Messages" Then
            MsgBox "Can't rename Large Messages folder!", vbInformation
            objExpl.CurrentFolder.Name = "Large Messages"
        End If
    End If
End Sub

FolderRemove

The FolderRemove event occurs when a folder is deleted from its Folders collection object, either through user action or program code. Unlike the FolderAdd and FolderChange events, FolderRemove does not pass a MAPIFolder object for the folder that has been removed. This example is similar to the code examples for the FolderAdd and FolderRemove events. It requires that you chain a FolderRemove event for the Inbox to a FolderAdd event for the Deleted Items folder. In this instance, if a folder is deleted from the Inbox and the deleted folder contains items, the deleted folder is displayed in an Explorer window.
'Place these declarations in the general section of ThisOutlookSession
Public WithEvents colDeletedItemsFolders As Outlook.Folders
Public WithEvents colInboxFolders As Outlook.Folders
Public blnDelete As Boolean

Private Sub Application_Startup()
    Set objNS = Application.GetNameSpace("MAPI")
    Set colInboxFolders = objNS.GetDefaultFolder(olFolderInbox).Folders
    Set colDeletedItemsFolders = _
      objNS.GetDefaultFolder(olFolderDeletedItems).Folders
End Sub
Private Sub colInboxFolders_FolderRemove()
    blnDelete = True
End Sub

Private Sub colDeletedItemsFolders_FolderAdd(ByVal Folder As MAPIFolder)
    If blnDelete And Folder.Items.Count Then
        Folder.Display
    End If
    blnDelete = False
End Sub

2 comments:

  1. Hello! I'm at work surfing around your blog from my new iphone! Just wanted to say I love reading through your blog and look forward to all your posts! Keep up the fantastic work!

    Feel free to visit my website - future quotes

    ReplyDelete
  2. I am sure this post has touched all the internet visitors, its really really good article on
    building up new website.

    Here is my website ... best quotes ever

    ReplyDelete