?
Solved

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

Posted on 2004-03-25
6
Medium Priority
?
590 Views
Last Modified: 2013-12-25
Hi.

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
            objMessage.Recipients.Resolve
            ' 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
                objMessage.Send
                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.
0
Comment
Question by:johnhyde
[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
  • 3
6 Comments
 
LVL 14

Expert Comment

by:JohnK813
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.
0
 

Author Comment

by:johnhyde
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,

John.
0
 
LVL 14

Accepted Solution

by:
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
Else
  iAddr.DisplayName = strEmail
EndIf
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
  .Configuration.Fields.Update
  .AddAttachment("c:\path\file.txt")
  .Send
End With

Hope this helps.  I'll be happy to answer any questions you have.
0
Independent Software Vendors: 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:johnhyde
ID: 10688092
Many thanks - I'll have a play with it and get back to you - hopefully Monday at the latest.

John.
0
 

Author Comment

by:johnhyde
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.

John.
0
 
LVL 14

Expert Comment

by:JohnK813
ID: 10688872
Glad to see everything worked smoothly.

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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 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…
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…
Suggested Courses

770 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