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

Posted on 2004-03-25
Medium Priority
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 (john.hyde@whatever.net, 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 john.hyde@whatever.net, 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
  • 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 http://msdn.microsoft.com, 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 2000 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.
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.


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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As a person who answers a lot of questions, I often see code that could be simplified, made easier to read, and perhaps most importantly made easier to maintain if the code was modified to use the Select Case statement. This article explains how to…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

600 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