Email validation in VBA

I found another website that lists web services you can consume in your VBA applications. WebserviceX.net

provides On Demand XML Web Services for Financial,Distribution, Retail, Health Care, Manufacturing, Telecom, Government and Educational Industry.

What this means for you is more stuff to do. Well, more stuff for your applications to do.

Let's use one web service: validating email addresses. I'm not talking about the formatting, but actually checking if the email address can be mailed.

The API at WebserviceX explains how to use it. Here's how the code works out:

Function IsValidEmail(emailAddress As String) As Boolean

Dim xml As Object
Dim result As String
  Set xml = GetMSXML

  xml.Open "GET", "http://www.webservicex.net/ValidateEmail.asmx/IsValidEmail?Email=" & emailAddress, False
  xml.Send

  result = xml.responsetext

  ' parse result for response

  IsValidEmail = (InStr(result, "true") > 0)
End Function

The XML response looks like this:

<?xml version="1.0" encoding="utf-8"?>
<boolean xmlns="http://www.webservicex.net">true</boolean>

The service returns true if the email address is valid; false if it isn't. So all we have to do is check for those in the result.

For the GetMSXML function, visit MSXML Object Library Routines.

Sample usage

Sub TestValidEmail()
  Debug.Print IsValidEmail("asdf@asdf.com")
End Sub

I wonder, though, if this is a legit use for VBA?

About JP

I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space to learn more about VBA. Keep Reading »



Share This Article:

Share and bookmark this articledelicious buttonfacebook buttonlinkedin buttonstumbleupon buttontwitter button

comment bubble 4 Comment(s) on Email validation in VBA:

  1. Why isn't this a reasonable use for VBA?

    • I can imagine some lowly spammer using it to validate email addresses. But I suppose they can do it without my help.

  2. Pavlin Patel writes:

    I like this. Thanx for finding this.

  3. Thanks JP for another great post. Some Access Database users send emails to their customers and this code could be quite helpful for maintaining a list of valid email addresses.

This article is closed to any future comments.
learn excel dashboards