Accessing outlook contacts

J

John

Hi

I am trying to access outlook contacts folders and delete the contacts that
do not contain a certain category value in the categories field. 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
 
K

Ken Tucker [MVP]

Hi,

Here is a sample console app.

Imports System.Reflection

Imports Outlook = Microsoft.Office.Interop.Outlook

Module Module1

Sub Main()

' Create Outlook application.

Dim oApp As Outlook.Application = New Outlook.Application

' Get Mapi NameSpace.

Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")

oNS.Logon("YourProfileName", Missing.Value, False, True) ' TODO:

' Get Messages collection of Inbox.

Dim oInbox As Outlook.MAPIFolder =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)

Dim oItems As Outlook.Items = oInbox.Items

Console.WriteLine("Total : " & oItems.Count)

Console.WriteLine("Total Unread : " & oItems.Count)

' Loop each unread message.

Dim oContact As Outlook.ContactItem

Dim i As Integer

For i = 1 To oItems.Count

Try

oContact = DirectCast(oItems.Item(i), Outlook.ContactItem)

Console.WriteLine(i)

Console.WriteLine(oContact.FullName)

Catch

End Try

Console.WriteLine("---------------------------")

Next

' Log off.

oNS.Logoff()

' Clean up.

oApp = Nothing

oNS = Nothing

oItems = Nothing

oContact = Nothing

End Sub

End Module



Ken

-------------------

Hi

I am trying to access outlook contacts folders and delete the contacts that
do not contain a certain category value in the categories field. 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
 
J

John

Hi

I am getting a "Namespace or type 'Outlook' for the Imports
'Microsoft.Office.Interop.Outlook' cannot be found." error on line Imports
Outlook = Microsoft.Office.Interop.Outlook. Any idea what I am doing wrong?

Thanks

Regards
 
K

Ken Tucker [MVP]

Hi,

I would recomend using the office interop assemblies they solve some
problems with working with office. If you are not using them you can get
rid of that imports statement.

http://msdn.microsoft.com/library/d...ta/html/OfficePrimaryInteropAssembliesFAQ.asp

Ken
--------------------------------
Hi

I am getting a "Namespace or type 'Outlook' for the Imports
'Microsoft.Office.Interop.Outlook' cannot be found." error on line Imports
Outlook = Microsoft.Office.Interop.Outlook. Any idea what I am doing wrong?

Thanks

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top