Solved

Visual Basic Mapi/CDO contacts

Posted on 2004-09-01
8
590 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
Technology Partners: 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 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

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!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

751 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