0

I tried this code to find contacts in GAL by jobtitle,

Sub FindEmailsByJobTitle()
    Dim olApp As Outlook.Application
    Dim oGAL As Outlook.AddressList
    Dim oContacts As Outlook.AddressEntries
    Dim oContact As Outlook.ContactItem
    Dim oResults As Outlook.Items
    Dim sFilter As String
    Dim sJobTitle As String
    Set olApp = Outlook.Application
    Set oGAL = olApp.Session.AddressLists("Global Address List")
    sJobTitle = "MANAGER"
    sFilter = "@SQL=" & Chr(34) & "urn:schemas:contacts:JobTitle" & Chr(34) & " LIKE '%" & sJobTitle & "%'"
    
    Set oResults = oGAL.AddressEntries.GetFirst().AddressList.Find(sFilter) 'this is error
    
    If Not oResults Is Nothing Then
        For Each oContact In oResults
            Debug.Print oContact.Address
        Next oContact
    Else
        MsgBox "No contacts found."
    End If
    
    Set oResults = Nothing
    Set oContacts = Nothing
    Set oGAL = Nothing
    Set olApp = Nothing

End Sub

but throw an error with error message "Object doesn't support this property or method"

thanks for help ^_^

DecimalTurn
  • 3,243
  • 3
  • 16
  • 36
Iws
  • 1
  • 1

2 Answers2

0

AddressEntries object does not support Find method. Generally, search support by the address containers is very basic compared to searching in message store folders (Items.Find/FindNext/Restrict).

The best you can do in OOM is Namespace.CreateRecipient / Recipient.Resolve and hope that the given value can be uniquely resolved.

If using Redemption (I am its author) is an option, you can filter on a few properties explicitly exposed by GAL for searching (Alias, City, Company, Department, First Name, Last Name, Office, Title). This is what you see in the address book window in Outlook if you click "Advanced Find". Redemption exposes it through the RDOAddressListSearch object:

set rSession = CreateObject("Redemption.RDOSession")
rSession.MAPIOBJECT = Application.Session.MAPIOBJECT
set rAddrList = rSession.Addressbook.GAL
set rSearch = rAddrList.Search
rSearch.Title = "MANAGER"
set rAddressEntries = rSearch.GetResults
for each rAddressEntry in rAddressEntries
    Debug.Print rAddressEntry.Name
next
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

The AddressList class doesn't provide the Find method. You need to iterate over all GAL entries manually or just create a recipient by using the CreateRecipient method of the Namespace class. This method is most commonly used to create a Recipient object for use with the GetSharedDefaultFolder method, for example, to open a delegator's folder. It can also be used to verify a given name against an address book. The name of the recipient can be a string representing the display name, the alias, or the full SMTP email address of the recipient. Then you can get a Recipient object instance and get the email address.

Sub ResolveName() 
 Dim myNamespace As Outlook.NameSpace 
 Dim myRecipient As Outlook.Recipient 
 Dim CalendarFolder As Outlook.Folder 
 Set myNamespace = Application.GetNamespace("MAPI") 
 Set myRecipient = myNamespace.CreateRecipient("Eugene Astafiev")
 myRecipient.Resolve 
 If myRecipient.Resolved Then 
  MsgBox myRecipient.Address 
 End If 
End Sub 

Otherwise, you need to iterate over all items in the loop and check the JobTitle property of each entry.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45