PDA

View Full Version : Restrict deletion of mails



Jeevie
02-14-2011, 10:53 PM
Hi

Is there a way to restrict deletion of mails using VBA.

Thanks in advance

JP2112
02-24-2011, 07:46 AM
Sure, but anyone can just go to the VBA Editor and turn off the code.

Private WithEvents objExplorer As Outlook.Explorer
Private WithEvents msg As Outlook.MailItem

Private Sub Application_Startup()
Set objExplorer = Application.ActiveExplorer
End Sub

Private Sub objExplorer_SelectionChange()
If objExplorer.CurrentFolder.DefaultItemType = olMailItem Then
If objExplorer.Selection.Count > 0 Then
Set msg = objExplorer.Selection(1)
End If
End If
End Sub

Private Sub msg_BeforeDelete(ByVal Item As Object, Cancel As Boolean)

End Sub

JP2112
02-24-2011, 07:47 AM
Sure, but anyone can just go to the VBA Editor and turn off the code.

Put this code into the ThisOutlookSession module and restart Outlook.

Private WithEvents objExplorer As Outlook.Explorer
Private WithEvents msg As Outlook.MailItem

Private Sub Application_Startup()
Set objExplorer = Application.ActiveExplorer
End Sub

Private Sub objExplorer_SelectionChange()
If objExplorer.CurrentFolder.DefaultItemType = olMailItem Then
If objExplorer.Selection.Count > 0 Then
Set msg = objExplorer.Selection(1)
End If
End If
End Sub

Private Sub msg_BeforeDelete(ByVal Item As Object, Cancel As Boolean)
If MsgBox("delete?", vbYesNo) = vbNo Then
Cancel = True
End If
End Sub

SuperFerret
03-04-2011, 04:57 AM
Hi, don't normally tag onto the end of other folks threads but I am wanting to do the same and can't get this to work on my Outlook 2007.

We have someone keep deleting items from our group mailbox and I want a clear message to say "Please note - you should not delete ANY mail items" to discourage the culprit!

Never done VBA in Outlook so I may be doing something wrong, but I put the code in the ThisOutlookSession and restarted my PC (had to for another update anyway) but I can still delete? :help

JP2112
03-04-2011, 10:19 AM
Well, technically speaking, the code doesn't stop you from deleting, it only prompts you.

I compiled the code in Outlook 2003, you may want to compile it again to make sure it's correct. Also, restart Outlook if you make any changes.

Make sure you are selecting an email (not an appointment, task, note, delivery receipt etc). Also note that the default item type for the currently displayed folder must be MailItems.

What I would do is set a breakpoint in the SelectionChange and BeforeDelete events and see if they are firing (i.e. select a message and try to delete it). If the events do fire, then step through the code and see what is happening.

If the code executes but the message is deleted even if you select "No", it's possible that the objects or events changed in Outlook 2007.