Link to home
Start Free TrialLog in
Avatar of 2019 @LWH
2019 @LWH

asked on

Delete Outlook contacts based on a particular attribute (Add a condition)

Hi All

The below script deletes all contact objects in outlook where the mileage attribute = 500. Is there any way to add a condition to the script to prevent the contact from being deleted if it has any additional email addresses configured e.g. do not delete if a contact where mileage = 500 but has either email 2 or email 3 configured

Dim objOutlook
Dim objNamespace
Dim objFolder
Dim objItem
Dim objRecord

Set objOutlook = CreateObject("Outlook.Application")
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNameSpace.GetDefaultFolder(10)
Set objItem = objFolder.Items
Set objRecord = objItem.Find("[Mileage] = ""500""")

While Not objRecord Is Nothing
	
	If objRecord.Body = "" Then objRecord.Delete
	Set objRecord = objItem.FindNext

Wend

Set objRecord = Nothing
Set objItem = Nothing
Set objFolder = Nothing
Set objNamespace = Nothing
Set objOutlook = Nothing

Open in new window

C--Users-harrison.brooks-Desktop-Pro.PNG
Avatar of Bill Prew
Bill Prew

Didn't test, but should be like:

Dim objOutlook
Dim objNamespace
Dim objFolder
Dim objItem
Dim objRecord

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(10)
Set objItem = objFolder.Items
Set objRecord = objItem.Find("[Mileage] = ""500""")

While Not objRecord Is Nothing
      
      If objRecord.Body = "" And objRecord.Email2Address = "" And objRecord.Email3Address = "" Then
        objRecord.Delete
      End If
      Set objRecord = objItem.FindNext

Wend

Set objRecord = Nothing
Set objItem = Nothing
Set objFolder = Nothing
Set objNamespace = Nothing
Set objOutlook = Nothing

Open in new window


»bp
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.