Need to resolve names using CDOEX to send e-mails in a VB6 program

Posted on 2004-03-25
Last Modified: 2013-12-25

I'm amending a VB6 program I inherited that used to use CDO 1.21 to either print letters or to send attachments via e-mail.  Since we've moved to Windows 2000 I need to upgrade this to CODEX.  (It's form, not Web based).

The program works OK if I give it a full e-mail address (, for example).  However, the old program included code to resolve the address if it was entered as a name in the users (Outlook 2000) address book.  So if the user had an address book entry for john hyde pointing to, the user could enter an address of "john hyde", Jo Hyd" or anything else that Outlook would normally resolve.  This was accomplished with a .Recipients.Resolve" as follows:

    Dim objMessage              As MAPI.Message
            Set objRecipient = objMessage.Recipients.Add(txtEmail.Text)
            ' If the txtCopiesTo text box is not empty then add this recipient too
            If txtCopiesTo.Text <> "" Then
                Set objRecipient = objMessage.Recipients.Add(txtCopiesTo.Text)
                objRecipient.Type = olCC
            End If
            ' On error - go to the CDO error handling which traps errors pertaining
            ' to email recipient resolution
            strLabel = "Send11"
            On Error GoTo CDO_Error
            ' Resolve the email addresses
            ' Reset back to Normal error handling
            On Error GoTo Error_Label
            ' Cycle through the recipients (currently just two) to extract the
            ' resolved email addresses and place them in the Correspondence form
            strLabel = "Send12"
            For Each objRecipient In objMessage.Recipients
                Select Case objRecipient.Type
                    Case olTo
                        Me.txtEmail.Text = objRecipient.AddressEntry
                    Case olCC
                        Me.txtCopiesTo.Text = objRecipient.AddressEntry
                    Case Else
                        ' Do nothing
                End Select
            Next objRecipient
            ' Ask the user if they want to send the email to the resolved recipients.
            ' If so, the correspondence is then saved to history, and sent via email.
            strLabel = "Send13"
            If vbYes = MessageConfirm(503, txtEmail.Text, txtCopiesTo.Text) Then
                Call SaveToHistory
                MessageConfirm (504)
            End If

There is no ".Recipients" property available using CDOEX, so the question is - can I emulate this code using CDOEX for users who have Outlook loaded on their machines, or is CDOEX so removed from the Outlook interface that this is no longer possible/practical?  (If it is possible, would the same code work for people with Outlook Express?).

As a tack-on question, I'll also need to send Fax messages using the same program.  Can anyone point me to some code to do this using CDOEX?  (We have Zetafax set up on site - can we simply send a file to the Zetafax printer?)

A speedy answer would be appreciated - hence the 500 point allocation.

With thanks in anticipation,

John Hyde.
Question by:johnhyde
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
  • 3
LVL 14

Expert Comment

ID: 10678639
Do you use an Active Directory?  If so, you can use an iAddressee (information can be found on, search for "CDOEX iAddressee".)  I have some code I can show you if this is that case.

As for using CDOEX to fax, I don't think I can help you there.

Author Comment

ID: 10686239
Thanks for this.  Yes, we do use Active directory.  However, we're on Windows 2000 and Exchange Server 2000, and looking up "iAddressee" on MSDN only brings up references to Exchange Server 2003.  Is that going to rule this solution out?

If you think it doesn't, I'd appeciate seeing your code.

Thanks again,

LVL 14

Accepted Solution

JohnK813 earned 500 total points
ID: 10687019
We use Win2000 and Exchange 2000 also.  I think MSDN just put everything under Exchange 2003 to save space.

(Don't forget to reference CDOEX - I always forget to do this at first.)

Here's your setup:

Dim Info 'Active Directory Info
Dim iAddr
Dim strEmail 'email or name entered
Dim strGoodEmail 'valid email to use
Dim oMessage as CDO.Message
Set Info = CreateObject("ADSystemInfo")
Set iAddr = CreateObject("CDO.Addressee")

The five basic iAddress commands you will need are:
iAddr.EmailAddress - holds email address
iAddr.DisplayName - holds corresponding name
iAddr.CheckName - retrieves email/name
iAddr.ResolvedStatus - returns CheckName results
iAddr.AmbiguousNames - collection of matches from CheckName, if more than one returned.  This will come in to play, for instance, when you type in "John H" and there are multiple Johns with last names that start with H.

DisplayName doesn't require an entire name, so you can use "John H" or "J Hyde".
CheckName basically "fills in the gaps."  If you give it an EmailAddress, it'll give you the DisplayName, and vice-versa.

So, what you can do is first check whether the string entered was an email address or just a name:
If instr(strEmail,"@") Then
  iAddr.EmailAddress = strEmail
  iAddr.DisplayName = strEmail
iAddr.CheckName("LDAP://" & Info.DomainDNSName) 'this checks the Active Directory
Select Case iAddr.ResolvedStatus
  Case 1 'valid email found
    strGoodEmail = iAddr.EmailAddress
  Case 2 'multiple matches found
    'what I normally do here is set up a For...Each
    'statement to loop through
    'iAddr.AmbigousNames, then display them in a
    'MsgBox, Exit Sub, and say "Try Again".
    'There's really not much else you can do.
  Case 3 'no matches
    'tell user to try again
End Select

Now, it's time to send the email

With oMessage
  .To = strGoodEmail
  .From = [your email]
  .TextBody = "body of email"
  .Subject = "Subject"
  'I use SMTP port 25 to send emails through
  'Exchange.  MSDN explains other methods.
  .Configuration.Fields(cdoSendUsingMethod) = cdoSendUsingPort
  .Configuration.Fields(cdoSMTPServer) = "ExchangeServerNameHere"
  .Configuration.Fields(cdoSMTPServerPort) = 25
End With

Hope this helps.  I'll be happy to answer any questions you have.
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

ID: 10688092
Many thanks - I'll have a play with it and get back to you - hopefully Monday at the latest.


Author Comment

ID: 10688520
Wow!  Didn't expect that to be so painless.  I virtually dropped the code in and it works fine.  Great code, clear as a bell.  

Many thanks for your help - it's well worth the points, and it's save me days of hassle.

LVL 14

Expert Comment

ID: 10688872
Glad to see everything worked smoothly.

I guess we're both lucky that I just happened to be working on something similar.

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

Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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 lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 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