Results 1 to 5 of 5
  1. #1
    3 Star Lounger
    Join Date
    May 2001
    Location
    Kenilworth, Warwickshire, England
    Posts
    269
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Selecting outlook contacts by string in category

    Hello,

    I have some ageing vba code that has worked for a long time. It is part of a process I use to select outlook contacts and then write specific fields out to an excel file. I have always been able to select on contents of a user generated field, but now have to select on a string which could appear in a number of categories. It is possible that some of the contacts have more than one of these categories and I would only want the record to appear once. Category(ies) is not one of the fields I wish to write out.

    The existing code that selects looks like

    For Each itm In fld.Items
    If itm.Class = olContact Then
    'Process item only if it is a contact item
    If itm.Account = "Both" Or itm.Account = "Croquet" Or itm.Account = "MC Croquet" Then
    ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx then
    i = i + 1

    What I would really appreciate is a line or lines of code that would substitute for the xxxxxxxxxxxxxxxx's and achieve the desired result. I have tried "like" and "filter", cant find "contains" without but I do not know whether it is because my coding is useless or it just doesn't apply to categories.
    If you can help, please be explicit about any variables etc that you are defining - I am very rusty!

    Thanks

    Mike C

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Does this reference help?
    https://msdn.microsoft.com/en-us/lib.../ff869662.aspx

    It suggests you use InStr to reveal if your search string appears in the Categories field.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    3 Star Lounger
    Join Date
    May 2001
    Location
    Kenilworth, Warwickshire, England
    Posts
    269
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hello Andrew,

    thank you for the direction. I'm no coder and while the reference you gave me reads as though Instr is where I should be looking, my feeble attempts to find the right syntax have failed miserably.

    So, in a contact folder, with an object itm declared, and with fld being an outlook folder

    'Iterate through contact items in Contacts folder, and export a few fields
    'from each item to a row in the Contacts worksheet
    For Each itm In fld.Items
    If itm.Class = olContact Then
    'Process item only if it is a contact item
    ....................this works!

    originally I could test on the contents of a regular field

    ' If itm.Account = "Both" Or itm.Account = "Croquet" Or itm.Account = "MC Croquet" Or itm.Account = "" Or itm.Account = "Account" Then
    .........................also worked fine, but now life has got more complicated and I can only find the set of records by using categories; there are several variations on the categories, but they all contain the string "Croquet! ( is this test case sensitive?)

    Your suggested material told me that categories is a field with all the categories in it, comma space delimited, held as a text field

    So I need something which, described using English, feels like
    If itm.categories contains the string "Croquet" anywhere then continue with this itm
    i = i + 1

    but I just can't get the syntax right.

    May I ask if you can help?

    Thanks

    Mike C

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts
    I don't know what processing you want to do but this code will show you how to pick the contacts with a Croquet category
    Code:
    Public Sub GetListOfContacts()
      Dim Session As Outlook.NameSpace
      Dim ContactFolder As Outlook.Folder
      Dim currentItem As ContactItem
      
      Set Session = Outlook.Session
      Set ContactFolder = Session.GetDefaultFolder(olFolderContacts)
      For Each currentItem In ContactFolder.Items
        If (currentItem.Class = olContact) Then
          If InStr(currentItem.Categories, "Croquet") > 0 Then
            Debug.Print currentItem.FullName, currentItem.Categories
          End If
        End If
      Next
    End Sub
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    Mike C (2016-01-24)

  6. #5
    3 Star Lounger
    Join Date
    May 2001
    Location
    Kenilworth, Warwickshire, England
    Posts
    269
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hello Andrew,

    So, I have discovered that Instr is case sensitive!

    I was only the operator away from the solution, but a miss is a as good as a mile...

    Thanks

    Mike C

Posting Permissions

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