?
Solved

Visual Basic Mapi/CDO contacts

Posted on 2004-09-01
8
Medium Priority
?
596 Views
Last Modified: 2013-11-25
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


0
Comment
Question by:nkjohnson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
8 Comments
 
LVL 1

Assisted Solution

by:deane_barker
deane_barker earned 500 total points
ID: 11955104
Are you trying to do this in a client program?  I've always found it easiest to remote Outlook instead of messing with CDO.  Here's a script that will iterate every item in the Contacts folder (or any folder, really) in VBScript/VBA using a function from Sue of Slipstick.com fame.  Shouldn't be hard to re-purpose this to VB.


Set oContactsFolder = GetFolder("Mailbox - Deane Barker\Contacts")
For Each oItem in oContacts Folder

    'Do Stuff Here

Next

'This function was provided by Sue Mosher in a couple of her books and on her Web site -- I didn't write it
Function GetFolder(strFolderPath)

    strFolderPath = Replace(strFolderPath, "/", "\")
    arrFolders = Split(strFolderPath, "\")
    Set objApp = CreateObject("Outlook.Application")
    Set objNS = objApp.GetNamespace("MAPI")
    Set objFolder = objNS.Folders.Item(arrFolders(0))

    If Not objFolder Is Nothing Then

        For I = 1 To UBound(arrFolders)

            Set colFolders = objFolder.Folders
            Set objFolder = Nothing
            Set objFolder = colFolders.Item(arrFolders(I))
            If objFolder Is Nothing Then

                Exit For
           
            End If

        Next

    End If

    Set GetFolder = objFolder
    Set colFolders = Nothing
    Set objNS = Nothing
    Set objApp = Nothing

End Function
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 11959736
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
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 11960604
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:nkjohnson
ID: 11963375
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.


0
 
LVL 50

Accepted Solution

by:
Dave Brett earned 500 total points
ID: 11979265
Hi,

I'm running the code above succesfully from Excel

Have you tried other methods of returning the contact rather than MsgBox?

Cheers

Dave
0
 

Author Comment

by:nkjohnson
ID: 12680437
I apologize for the delay.

Thanks for the reminder.

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question