Results 1 to 12 of 12
  1. #1
    New Lounger
    Join Date
    Dec 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using CDO in VB code to get fields in GAL (Word/Ou

    I want to access several fields for names listed in the GAL, such as
    Department, Phone, etc. I found this sample code for Visual Basic, and
    modified a few little things. I confirmed that CDO 1.2.1 is installed, and I
    enabled the checkbox in Tools/References in my VB project.

    FYI - code runs fine thru this command:
    Debug.Print olAddrEntries.Item(i).Name

    Error occurs at this line in the code -
    Debug.Print olAddrEntries.Item(i).Fields(CdoPR_DEPARTMENT_NAME ).Value

    Here is the code:

    Public Sub RetrieveStaffInfo()

    Dim objOutlook
    Dim olNS
    Set objOutlook = CreateObject("Outlook.application")
    Set olNS = objOutlook.GetNameSpace("MAPI")

    'Get address book list
    Dim olAddrEntries
    Set olAddrEntries = olNS.AddressLists.Item(1).AddressEntries

    Dim i As Integer

    'Output names of those in the Global Address Book
    For i = 1 To olAddrEntries.Count

    Debug.Print olAddrEntries.Item(i).Name
    Debug.Print olAddrEntries.Item(i).Fields(CdoPR_DEPARTMENT_NAME ).Value
    Next

    'Cleanup
    Set olNS = Nothing
    Set olAddrEntries = Nothing

    End Sub


    I appreciate any advice!! (I've already been to CDOLive and several other
    resources

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Using CDO in VB code to get fields in GAL (Word/Ou

    What does the error message say?

  3. #3
    New Lounger
    Join Date
    Dec 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using CDO in VB code to get fields in GAL (Wor

    Error says "Runtime Error 438" "Object does not support this property or method"

    If you have access to an exchange server with Global Address List, you can copy & paste the code I posted and test it, yes?

    Thank you, Hans.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Using CDO in VB code to get fields in GAL (Wor

    I won't be able to test on Exchange until next Monday, so I hope that someone else will reply before that.

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Using CDO in VB code to get fields in GAL (Word/Ou

    When I run this code, it accesses my Contacts. So that's the first problem. I added a little loop to get the GAL:

    <code>Dim intCount As Integer
    With olNS.AddressLists
    For intCount = 1 To .Count
    If .Item(intCount).Name = "Global Address List" Then _
    Set olAddrEntries = olNS.AddressLists.Item(intCount).AddressEntries
    Next
    End With</code>


    But, I get an error (same place you do) because the Fields collection is not exposed (I looked in the Local window to see what I could see) Outlook is treating the address entry as an object of type Outlook.AddressEntry. Perhaps you need to access it as some other type of object in order to get the Fields collection?

  6. #6
    New Lounger
    Join Date
    Dec 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using CDO in VB code to get fields in GAL (Wor

    Hi John, thanks for the reply and for testing the code. Yes, the "fields" collection is what I can't get into, you're right. I'll work with it some more this evening and will post my results. (You've helped me before, and saved me lots of confusion!)

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Using CDO in VB code to get fields in GAL (Wor

    <P ID="edit" class=small>(Edited by jscher2000 on 08-Jan-09 16:05. Oops, forgot to log off...)</P>Hi, try this. Note that I've created a MAPI session, and also used On Error Resume Next as a shortcut:

    <code>Public Sub RetrieveStaffInfo()
    ' Create Session using the MAPI Library, connect to current OL session
    Dim cdoSession As New MAPI.Session
    cdoSession.Logon , , False, False, 0 'See http://support.microsoft.com/kb/263572
    ' Retrieve the GAL
    Dim cdoGAL As MAPI.AddressEntries
    Set cdoGAL = cdoSession.AddressLists("Global Address List").AddressEntries
    ' Iterate over the GAL
    Dim i As Integer
    'Output names of those in the Global Address Book
    ' Suppress errors caused by blank or missing values
    On Error Resume Next
    ' Loop the GAL
    For i = 1 To cdoGAL.Count
    Debug.Print "Name: " & cdoGAL.Item(i).Name
    Debug.Print "Department: " & cdoGAL.Item(i).Fields(CdoPR_DEPARTMENT_NAME).Value
    Next
    ' Reset error handling
    On Error GoTo 0
    'Cleanup
    Set cdoGAL = Nothing
    cdoSession.Logoff
    Set cdoSession = Nothing
    End Sub</code>

  8. #8
    New Lounger
    Join Date
    Dec 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using CDO in VB code to get fields in GAL (Wor

    Yes, this works! So many code samples I tried to learn from, but I think there were some *assumed* things that I just didn't catch onto. Not a straightforward copy & paste situation for my exact situation. So I'm very grateful that you provided it for our project here. (I don't want to post it somewhere like CDO Live and take the credit - will leave that to you to do if you choose to)

    Best regards,
    Glynnis Elliott

  9. #9
    2 Star Lounger
    Join Date
    Jun 2001
    Location
    York, Pennsylvania, USA
    Posts
    164
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Re: Using CDO in VB code to get fields in GAL (Wor

    Speaking of MAPI.

    We're transitioning from Lotus Notes Mail to Exchange/Outlook 2007 under Windows XPPro.

    I have a third party application which accesses Outlook, or Notes mail, depending on how it's configured.

    I've now set it to access Outlook and, of course, it errors out when trying to create a MAPI object:

    Set objSession = CreateObject("MAPI.Session")

    No object is created.

    I'm wondering what we have to install or enable to give us MAPI access on the PC.

    Thanks

  10. #10
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Using CDO in VB code to get fields in GAL (Wor

    How did you DIm the MAPI Session? See <!post=this post,685,407>this post<!/post> from HansV.
    -John ... I float in liquid gardens
    UTC -7ąDS

  11. #11
    2 Star Lounger
    Join Date
    Jun 2001
    Location
    York, Pennsylvania, USA
    Posts
    164
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Re: Using CDO in VB code to get fields in GAL (Wor

    Hope this isn't a duplicate post. I just replied and don't see it. The CDO install did the trick. The actual problem was revealed when I stepped through the vendor's code. It appears they knew less than I do :>) Anyway, it's fixed and running well now.

  12. #12
    Lounger
    Join Date
    Oct 2003
    Location
    Toronto, Ontario, Canada
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Great topic, I have found good use for the result and it works great.

    Thanks...

    Stu Boasman

Posting Permissions

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