Solved

Visual Basic Mapi/CDO contacts

Posted on 2004-09-01
8
587 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 125 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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 125 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

740 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