Link to home
Start Free TrialLog in
Avatar of nkjohnson
nkjohnsonFlag for United States of America

asked on

Visual Basic Mapi/CDO contacts

Hi,

I am trying to pull data from my Oulook contacts folder using CDO.  I am very new to CDO, and have messed around with a couple of things, but keep getting errors.  The most popular one seems to be Error in Loading DLL.  Below is a snippet from my code.  Right now all I am trying to do is display a field by using the field name.  

Anyone have any ideas

Sub main()
Set objSession = CreateObject("MAPI.Session")
With objSession
    .Logon
    Set objAddressList = AddressBookExists("Contacts")  
    Set objAddressEntries = objAddressList.AddressEntries
    For Each objAddressEntry In objAddressEntries
               Msgbox objAddressEntry.Fields.Item(CDOPR_INITIALS)
    Next
End With
End Sub

Public Function AddressBookExists(sAddressBookName As String) As AddressList
    Dim objAddressList As AddressList
    For Each objAddressList In objSession.AddressLists
    If objAddressList.Name = sAddressBookName Then
        Set AddressBookExists = objAddressList
        Exit Function
    End If
    Next objAddressList
   
    Set objAddressList = Nothing
End Function


SOLUTION
Avatar of deane_barker
deane_barker

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dave
Maybe something like this... you willl need to update the code with your Mailbox details

Note that you can Logon automatically with
.Logon , , False, False

Cheers

Dave


Sub Main()
Dim objSession, oFolder, oMessage
    Set objSession = CreateObject("MAPI.Session")
    With objSession
        .Logon , , False, False
        Set oFolder = .InfoStores("Mailbox - Brett, David D").RootFolder
        Set oFolder = oFolder.Folders("Contacts")
        For Each oMessage In oFolder.Messages
            MsgBox oMessage.Fields.Item(CDOPR_INITIALS)
        Next
    End With
End Sub
More simply

Const CdoDefaultFolderContacts = 5

Sub Main2()
Dim objSession, oFolder, oMessage
   Set objSession = CreateObject("MAPI.Session")
   With objSession
       .Logon , , False, False
       Set oFolder = .GetDefaultFolder(CdoDefaultFolderContacts)
       For Each oMessage In oFolder.Messages
           MsgBox oMessage.Fields.Item(CDOPR_INITIALS)
       Next
   End With
End Sub
Avatar of nkjohnson

ASKER

brettdj  I am still getting the Error in loading DLL error on this line,

MsgBox oMessage.Fields.Item(CDOPR_INITIALS) :  Error # 48, Error in Loading DLL

It is pulling a Message because when I run a cursor over the message it gives me the first name in my contact folder.


ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I apologize for the delay.

Thanks for the reminder.