The code samples on this page ask for confirmation before a contact is deleted. If you accidentally delete a contact, you won't need to retrieve the contact from the Deleted items folder and move it back to the contacts folder.
The first code sample works with selected contacts, the second one works with opened contacts. You can use both if you need to, but I split them into separate macros because hitting the Delete key or Delete icon when viewing the contacts is easier to do.
To avoid the warning when making a copy of the contact or when moving a contact out of the deleted items folder, the macro checks the folder the contact is in and exits the sub if you are making a copy in the same folder. If you move or drag a contact to a new folder, you should not be asked to confirm "deletion" from the current folder. If you want to be warned when moving contacts, remove the line
If MoveTo.Name <> "Deleted Items" Then Exit Sub
Warn before deleting selected contact
The first two lines go at the top of the ThisOutlookSession module. The two Subs can be anywhere in ThisOutlookSession.
Private Sub objCurrentFolder_BeforeItemMove(ByVal Item As Object, ByVal MoveTo As MAPIFolder, Cancel As Boolean) If objCurrentFolder.DefaultItemType <> 2 Then Exit Sub If objCurrentFolder = MoveTo Then Exit Sub ' if using IMAP “Contacts (This computer only)" ' Change the folder name to Trash If MoveTo.Name <> "Deleted Items" Then Exit Sub StrName$ = Application.ActiveExplorer.Selection.Item(1).Subject 'prompt$ = "Are you sure to delete the contact for " & vbCrLf & StrName$ & "?" ' alternate prompt that includes the folder name the contact is moved to prompt$ = "Are you sure to move " & vbCrLf & StrName$ & " to the " & MoveTo.Name & " folder?" If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Confirm Contact Deletion") = vbNo Then Cancel = True End If End Sub
Warn before deleting open contact
If you need to use this macro and the one above, the first two lines go at the top of ThisOutlookSession module. The two Subs can be anywhere in ThisOutlookSession.
' opened contact Public WithEvents objInspectors As Outlook.Inspectors Public WithEvents olContactItem As Outlook.ContactItem Private Sub Application_Startup() Set objInspectors = Outlook.Application.Inspectors End Sub 'opened contact Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector) If Inspector.CurrentItem.Class = olContact Then Set olContactItem = Inspector.CurrentItem End If End Sub Private Sub olContactItem_BeforeDelete(ByVal Item As Object, Cancel As Boolean) prompt$ = "Are you sure to delete this Contact?" If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Confirm Contact Deletion") = vbNo Then Cancel = True End If End Sub
How to use the macros on this page
First: You need to have macro security set to the lowest setting, Enable all macros during testing. The macros will not work with the top two options that disable all macros or unsigned macros. You could choose the option Notification for all macros, then accept it each time you restart Outlook, however, because it's somewhat hard to sneak macros into Outlook (unlike in Word and Excel), allowing all macros is safe, especially during the testing phase. You can sign the macro when it is finished and change the macro security to notify.
To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, look at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Macros that run when Outlook starts or automatically need to be in ThisOutlookSession, all other macros should be put in a module, but most will also work if placed in ThisOutlookSession. (It's generally recommended to keep only the automatic macros in ThisOutlookSession and use modules for all other macros.) The instructions are below.
The macros on this page need to go into ThisOutlookSession.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the macro code in ThisOutlookSession:
- Expand Project1 and double click on ThisOutlookSession.
- Copy then paste the macro into ThisOutlookSession. (Click within the code, Select All using Ctrl+A, Ctrl+C to copy, Ctrl+V to paste.)
More information as well as screenshots are at How to use the VBA Editor