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 collnboxFolders 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 collnboxFolders 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 collnboxFolders = NewFolder.Parent.Folders End If End Sub
Private Sub collnboxFolders_FolderChange(ByVal Folder As MAPIFolder) If blnLargeMsgActive Then
If Folder.Name <> "Large Messages" Then
MsgBox "Can't rename Large Messages folder!", vblnformation objExpl.CurrentFolder.Name = "Large Messages" End If End If End Sub
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 colDeletedltemsFolders As Outlook.Folders Public WithEvents collnboxFolders As Outlook.Folders Public blnDelete As Boolean
Private Sub Application_Startup() Set objNS = Application.GetNameSpace("MAPI") Set collnboxFolders = objNS.GetDefaultFolder(olFolderlnbox).Folders Set colDeletedltemsFolders = _ objNS.GetDefaultFolder(olFolderDeletedltems). Folders End Sub
Private Sub collnboxFolders_FolderRemove()
blnDelete = True End Sub
Private Sub colDeletedltemsFolders_FolderAdd(ByVal Folder As MAPIFolder) If blnDelete And Folder.Items.Count Then
Folder.Display End If blnDelete = False End Sub
These examples are aimed at folders in a user's private information store where a Deleted Items folder exists. If you want to recover deleted items or folders from public folders programmatically, you will have to write Exchange 5.5 scripting agents or Exchange 2000 event sinks and have your Microsoft Exchange Server administrator enable deleted item recovery for the public information store using the Exchange Administrator program. You should be aware that a user can circumventthe Deleted Items folder by pressing Shift+Delete to delete an item or a folder. Additional details regarding deleted item restoration are provided in the following articles from the
Was this article helpful?