Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CDO or MAPI for email interface.

Status
Not open for further replies.

wxperson

Programmer
Feb 5, 2004
14
US
I need to write a VB program that will read the users address book.. display it and then allow them to send email with attachments.

I found MAPI code to send email with attachments however the only code I have found to display the addressbook needs CDO 1.21.

I have a couple of questions before I dig deeper.

1. Is there a way via MAPI to show the users address book (outlook I assume)?

2. There does not appear to be an option to select Microsoft CDO from my VB6 IDE. How can I install that object to VB6


Thanks,

George
 
CDO 1.21 comes with Outlook. It normally comes with Outlook; you can't distribute CDO with your app. See
Note that CDO is part of a default Outlook install in 98 and earlier but in 2000 and later it must be selected during a custom install.

Outlook doesn't provide a means of displaying the Address Book selector dialog. Use the AddressBook method of CDO Session object.

Paul Bent
Northwind IT Systems
 
Thanks for the reply.

I am trying to figure out how I can distribute my application to external customers. If they don't have CDO, my program which references it will probably bomb out on startup. Since this is a old application, I can't expect everyone who upgrades will even pay attention to the new prerequesite.. Also, many of my users are not very technical and have a hard time getting support.

DO you know what will happen with a CDO compiled project if it tries to run on an non CDO computer? Can I capture the error?

Thanks again.

George
 
Thanks.. Now I am looking for code that will read the users addressbook, including the email distribution lists.

If you know of any on the web... much appreciated.

George
 
Here's a rough example, I'm just typing this in:
Code:
Dim objSession As MAPI.Session
Dim objAddrList As MAPI.AddressList
Dim objAddrEntries As MAPI.AddressEntries
Dim objAddrEntry As MAPI.AddressEntry
Dim objDistMembers As MAPI.AddressEntries
Dim objDistMember As MAPI.AddressEntry

Set objSession = CreateObject("MAPI.Session")
objSession.Logon
'Get the GAL, can get the PAB the same way
Set objAddrList = objSession.GetAddressList(CdoAddressListGAL)
'Move through the users and distribution lists in the GAL
Set objAddrEntries = objAddrList.AddressEntries
Set objAddrEntry = objAddrEntries.GetFirst
Do Until objAddrEntry Is Nothing
 With objAddrEntry
  Select Case .DisplayTypoe
   Case CdoUser, CdoRemoteUser
    'Entry is a user
    Debug.Print "Display Name: " & .Name
    'SMTP e-mail address
    Debug.Print "E-mail address: " & .Fields(&H39FE0001E)
   Case CdoDistList
    'Move through the distribution list members
    Set objDistMembers = .Members
    Set objDistMember = objDistMembers.GetFirst
    Do Until objDistMember Is Nothing
     Debug.Print "Display Name: " & objDistMember.Name
     'SMTP e-mail address
     Debug.Print "E-mail address: " & objDistMember.Fields(&H39FE0001E)
     objDistMembers.GetNext
    Loop
  End Select
 End With
 Set objAddrEntry = objAddrEntries.GetNext 
Loop

Set objDistMember = Nothing
Set objDistMembers = Nothing
Set objAddrEntries = Nothing
Set objAddrEntry = Nothing
Set objAddrList = Nothing
Set objSession = Nothing

Paul Bent
Northwind IT Systems
 
Thanks for the code.. I really appreciate the start.

I tried it out and have 2 questions.

1. VB does not like the format of the debug statements
Debug.Print "E-mail address: " & objAddrEntry.Fields (&H39FE0001E)
The field in the parens is supposed to be a variant. Where does this hex # come from?

2. Even if I comment out the debug statements (just to make some progress).. I am getting a RUN TIME ERROR on the following line...
Set objAddrList = objSession.GetAddressList(CdoAddressListGAL)

The error reads.... Run Time error '-2147467259 (80004005)':
[collaboration Data Objects - [E_FAIL(800004005)]].

I hope that makes some sense to you because this stuff is new to me.

Any help to get past these 2 items is greatly appreciated.

Thanks,

George
 
I think I figured out #1..

Is the format objAddrEntry.Fields(&H39FE001E).Value correct?

Still need help with the Run time error.

Thx,

George
 
Yes, sorry about the .Value; I said I was just typing it in. :)

Are you connected to Exchange Server? If not there isn't a GAL. Use the constant CdoAddressListPAB to return the personal address book.

Paul Bent
Northwind IT Systems
 
I made the change to CdoAddresslistPAB.

Making progress...

Is the Debug.Print "E-mail address: " & objAddrEntry.Fields(&H39FE001E).Value statement still valid now.. It is coming up with a runtime error. [Collaboration Data Objects [ MAPI_E_NOT_FOUND(8004010F)]]

I added
Debug.Print "display Email address: " & .Address
right after the display name statement and commented out the offending line.. and it IS showing the correct email address for the entry. What info does the statement causing the error have in it?

Also.. It is not finding my PAB's distribution list however.

Thanks again for the help.

George
 
OK.. I made some changed to the code.. A little knowledge is a dangerous thing. Check the attached code out. I replaced the debug statments with the hex code in it with something that seems to work...

I also changed the case statement to make the distribution list a value of 5 (FIVE) when the entry is a distribution list.

The one remaining problem I am having is that the DO until loop for the distribution list never ends.. it finds the first entry over and over and over again... ALmost like the objDistMembers.GetNext statement is not working.

I you can take a quick look at what I have done and advise me on what I might be doing wrong, I would really appreciate it.

Thanks,

George


Dim objSession As MAPI.Session
Dim objAddrList As MAPI.AddressList
Dim objAddrEntries As MAPI.AddressEntries
Dim objAddrEntry As MAPI.AddressEntry
Dim objDistMembers As MAPI.AddressEntries
Dim objDistMember As MAPI.AddressEntry

Set objSession = CreateObject("MAPI.Session")
objSession.Logon ("Outlook")
'Get the GAL, can get the PAB the same way
Set objAddrList = objSession.GetAddressList(CdoAddressListPAB)
'Move through the users and distribution lists in the GAL
Set objAddrEntries = objAddrList.AddressEntries
Set objAddrEntry = objAddrEntries.GetFirst
Do Until objAddrEntry Is Nothing
With objAddrEntry
Select Case .DisplayType
Case CdoUser, CdoRemoteUser
'Entry is a user
Debug.Print "Display Name: " & .Name
Debug.Print "display Email address: " & .Address ' added by me
'SMTP e-mail address
' Debug.Print "E-mail address: " & objAddrEntry.Fields(&H39FE001E).Value
Case 5 ' changed by me
'Move through the distribution list members
Set objDistMembers = .Members
Set objDistMember = objDistMembers.GetFirst
Do Until objDistMember Is Nothing '
Debug.Print "Display Name: " & objDistMember.Name
'SMTP e-mail address
Debug.Print objDistMember.Address ' added by me
' Debug.Print "E-mail address: " & objDistMember.Fields(&H39FE001E).Value
objDistMembers.GetNext
Loop
End Select
End With
Set objAddrEntry = objAddrEntries.GetNext
Loop
Set objDistMember = Nothing
Set objDistMembers = Nothing
Set objAddrEntries = Nothing
Set objAddrEntry = Nothing
Set objAddrList = Nothing
Set objSession = Nothing
End Sub
 
Perhaps that field doesn't exist when Outlook is in IMO. With Exchange Server, the Address property returns the Exchange address in FQN form and the SMTP address is stored in that field. In IMO, there are only SMTP address so I guess they are stored in the Address property.

Try the constant CdoPrivateDistList to get distribution lists in your PAB.

Paul Bent
Northwind IT Systems
 
That did it. Thanks.

The final working code is below.

One "final" question however.. When I run this I get Microsoft outlook dialog box that says "A program is trying to access e-mail addresses you have stored in Outlook. DO you want to allow this? If this is unexpected, it may be a virus and you should choose NO". Then there is an option to allow access for XX minutes or say NO.

How can I make this dialog box NOT appear? It makes no sense for the user to have to deal with this everytime they run my application.

Thanks,

George


*****

Dim objSession As MAPI.Session
Dim objAddrList As MAPI.AddressList
Dim objAddrEntries As MAPI.AddressEntries
Dim objAddrEntry As MAPI.AddressEntry
Dim objDistMembers As MAPI.AddressEntries
Dim objDistMember As MAPI.AddressEntry
Set objSession = CreateObject("MAPI.Session")
objSession.Logon ("Outlook")
'Get the PAB
Set objAddrList = objSession.GetAddressList(CdoAddressListPAB)
'Move through the users and distribution lists in the PAB
Set objAddrEntries = objAddrList.AddressEntries
Set objAddrEntry = objAddrEntries.GetFirst
Do Until objAddrEntry Is Nothing
With objAddrEntry
Select Case .DisplayType
Case CdoUser, CdoRemoteUser
'Entry is a user
Debug.Print "Address book Name and email address: " & .Name & ", " & .Address
Case CdoPrivateDistList
'Move through the distribution list members
Set objDistMembers = .Members
Set objDistMember = objDistMembers.GetFirst
Debug.Print "Distribution list name: " & objAddrEntry.Name
Do Until objDistMember Is Nothing '
Debug.Print " Distribution list member Name and email address: " & objDistMember.Name & ", " & objDistMember.Address
Set objDistMember = objDistMembers.GetNext
Loop
End Select
End With
Set objAddrEntry = objAddrEntries.GetNext
Loop
Set objDistMember = Nothing
Set objDistMembers = Nothing
Set objAddrEntries = Nothing
Set objAddrEntry = Nothing
Set objAddrList = Nothing
Set objSession = Nothing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top