Solved

Visual Basic Mapi/CDO contacts

Posted on 2004-09-01
8
575 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
  • 3
  • 2
8 Comments
 
LVL 1

Assisted Solution

by:deane_barker
deane_barker earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:nkjohnson
Comment Utility
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
Comment Utility
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
Comment Utility
I apologize for the delay.

Thanks for the reminder.

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now