Consulting

Results 1 to 4 of 4

Thread: Contact Group causing error

  1. #1

    Contact Group causing error

    The following code runs through each contact in the default folder. Problem is that when it comes across a Contact Group the 'Next olContact' statement creates a Type Mismatch with olContact = Nothing.

    How can I prevent this error? Is it possible to filter out the Contact Groups?

    Set olApp = Outlook.Application
    Set objName = olApp.GetNamespace("MAPI")
    Set olFolder = objName.GetDefaultFolder(olFolderContacts)
    Set olContact = olFolder.Items.GetFirst

    For Each olContact In olFolder.Items

    With olContact

    ' various actions on contact take place here
    End With

    Next olContact

    Thank you.

    Peter R Hawkes

  2. #2
    remember that folder your script is targeting must use OAB for the name resolution to work in distro lists
    Try to set MAPIFolder.ShowAsOutlookAB property to true

  3. #3
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    As you've found, the Contacts folder can hold other items besides Contacts (DistListItems being one). Compare each item in the folder to the ContactItem type using TypeName or Type Of before attempting an operation on a ContactItem.

    [VBA]
    Dim olApp As Outlook.Application
    Dim objName As Outlook.Namespace
    Dim olFolder As Outlook.MAPIFolder
    Dim obj As Object
    Dim olContact As Outlook.ContactItem
    Dim i As Long

    Set olApp = Outlook.Application
    Set objName = olApp.GetNamespace("MAPI")
    Set olFolder = objName.GetDefaultFolder(olFolderContacts)

    For i = 1 to olFolder.Items.Count
    ' grab each item
    Set obj = olFolder.Items(i)
    ' is it a ContactItem?
    If TypeName(obj) = "ContactItem" Then
    ' use a typecast variable and continue with operation
    Set olContact = obj
    With olContact
    ' various actions on contact take place here
    End With
    End If
    Next i
    [/VBA]
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  4. #4
    Excellent, all now working as expected and personal KB increased!

    Thank you.

    Peter H

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •