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: colDeletedltemsFolders and collnboxFolders. When a folder is deleted from collnboxFolders, the FolderRemove event fires. The global variable blnDeleteRestore is set to True in the FolderRemove event procedure for collnboxFolders. When a folder is deleted from collnboxFolders, it is moved to the Deleted Items folder. The FolderAdd event procedure for thecolDeletedltemsFolders 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 colDeletedltemsFolders As Outlook.Folders Public WithEvents collnboxFolders As Outlook.Folders Public blnDeleteRestore 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()
blnDeleteRestore = True End Sub
Private Sub colDeletedltemsFolders_FolderAdd(ByVal Folder As MAPIFolder) If blnDeleteRestore Then
MsgBox "You cannot delete " & Folder.Name _ & vbCr & "This folder will be restored to your Inbox.", _ vb Information
Folder.MoveTo objNS.GetDefaultFolder(olFolderlnbox) End If blnDeleteRestore = False End Sub
Was this article helpful?