Removing contact items

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I need to delete, via code, all contacts whose category field has a certain
value (lets say 'xxx') in it. What is the code that I need to use for this?

Thanks

Regards
 
Hi John,

you can loop per For Each through the folders and check the Categories
property with the Instr$ function. For better performance I´d do that
via CDO instead of the OOM.
 
Hi

I have written the below code but am stuck at how to check for the condition
and how to delete individual contact that fits the condition. Any help would
be appreciated.

Thanks

Regards


Dim O As Outlook.Application
Dim F As Outlook.MAPIFolder

O = New Outlook.Application

F = O.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)

ICount = F.Items.Count
For I = 1 To ICount
' Stuck here and need code help for: If categories field contains
category value 'x' then
' delete this contact
Next I
 
Hi John,
O = New Outlook.Application

for setting an object variable you must write the Set instruction:

Set O = ...
For I = 1 To ICount
' Stuck here and need code help for: If categories field contains
category value 'x' then
' delete this contact
Next I

You could work with Items(I) in this case. Anyway, for object references
I´d recommend For Each:

Dim obj as Object ' Because a folder can contain different object types
_
you should declare the variable as a general object.
Dim oContact as Outlook.ContactItem

For Each obj in F.Items
If Typeof obj is Outlook.ContactItem Then
Set oContact=obj
' Now you can handle the ContactItem, use Intellisense
Endif
Next

For checking the condition please read the VBA help for the InStr
function. You could use the Object Browser, switch from <All Libraries>
to Outlook, select "ContactItem" in the left pane and "Categories" in
the right. Then press F1 for the VBA help and a code sample.
 
Hi Michael

Many thanks for that. Actually I am doing all this in vb.net hence the
slight syntax change. I have
written the below code but am stuck with the error on the indicated line.
Any help would be appreciated.

Thanks

Regards


Dim O As Outlook.Application
Dim F As Outlook.MAPIFolder
Dim ICount As Long
Dim oContact As Outlook.ContactItem
Dim obj As Outlook.ContactItem

O = New Outlook.Application
F = O.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
For Each obj In F.Items() ' *** This line gives error: Expression is of
type 'Outlook.Items', which is not a collection type.
If TypeOf obj Is Outlook.ContactItem Then
oContact = obj
If InStr(oContact.Categories, "mycat") Then
oContact.Delete()
End If
End If
Next
 
Hi John,

as I mentioned, the folder can contain different object types
(ContactItem and DistListItem). Due to that you´d need to declare the
variable, used in For-Each, as an general object.
 
Back
Top