+ Reply to Thread
Results 1 to 7 of 7

Exception Handling of invalid email address

Hybrid View

  1. #1
    Registered User
    Join Date
    10-11-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    21

    Exception Handling of invalid email address

    Hi,

    I am trying to email a list of people using Macro. I have the list in a column and I am looping through the column to email each one with a specified message coming from another column on the same worksheet.

    I do have the email macro going well but I have a question. If the email is incorrect, the macro halts on that email. I want to do some exception handling for the same. Can you suggest something ?

    Also I am a newbie to the excel macro world, so i am pasting my code for your verification and suggestion for modification and enhancement.

    
    Sub Send_Excel_Cell_Content_To_Lotus_Notes()
    
    Dim Notes As Object
    Dim WorkSpace As Object
    Dim UIdoc As Object
    Dim UserName As String
    Dim PI_name As String
    Dim i As Integer
    
    i = 0
    On Error Resume Next
    
    ThisWorkbook.Sheets("EMAIL_LIST").Activate
    
    Cells.Find(What:="PI_Name", LookIn:=xlValues, MatchCase:=False).Activate
    
    ActiveCell.Offset(1, 0).Activate
    
    PI_name = ActiveCell.Value
    
    ActiveWorkbook.Names.Add name:="Bookmark", RefersTo:=ActiveCell
    
    Do While PI_name <> ""
        
        Set Notes = CreateObject("Notes.NotesSession")
        UserName = Notes.UserName
        
        Set WorkSpace = CreateObject("Notes.NotesUIWorkspace")
        
        Call WorkSpace.ComposeDocument(, , "Memo")
        
        Set UIdoc = WorkSpace.CurrentDocument
        
        ' Recipient = ThisWorkbook.Sheets("EMAIL_LIST").Range("A2").Value
        Recipient = ActiveCell.Offset(1, 1).Value
        
        Call UIdoc.FieldSetText("EnterSendTo", Recipient)
        
        Subject1 = "Testing from Macro"
        Call UIdoc.FieldSetText("Subject", Subject1)
        
        Call UIdoc.GotoField("Body")
        Body1 = "Hi This is a test"
        Call UIdoc.InsertText(Body1)
        
        'Insert some carriage returns at the end of the email
        Call UIdoc.InsertText(vbCrLf & vbCrLf)
        
        UIdoc.send (True)
        UIdoc.Close
         
        Set UIdoc = Nothing: Set WorkSpace = Nothing: Set Notes = Nothing
        
        Application.Goto Reference:="Bookmark"
        i = i + 1
        PI_name = ActiveCell.Offset(i, 0).Value
        
    Loop
    
    End Sub

  2. #2
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Exception Handling of invalid email address

    In general the way that you do exception handling is
    On Error Resume Next ' turn on error handling
    ' perform some function
    If (Err.Num <> 0) Then
        ' do the error handling here
    Else
        ' do normal processing
    End If
    On Error GoTo 0 ' turn off error handling
    wrap this around the code you want to handle. Hope this helps.
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

  3. #3
    Registered User
    Join Date
    10-11-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Exception Handling of invalid email address

    Can you tell me when i use a cells.find and if the value is not found how to do the error handling ?

    Also, what does this do

    If Not c Is Nothing Then c.Select

    c is defined as range

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Exception Handling of invalid email address

    Hello ncduke,

    In this case, you don't need error handling. The range Find method returns one of two possible values. If the value is found then the cell, itself a Range object, will be returned. If not then the special Object value Nothing is returned.

    By assigning a range object variable to the Find operation, you can then test the result and avoid an error. I have made the needed changes to your code and included comments. Here is the revised macro.
    Sub Send_Excel_Cell_Content_To_Lotus_Notes()
    
    Dim NameRange As Range
    Dim Notes As Object
    Dim RngRnd As Range
    Dim WorkSpace As Object
    Dim UIdoc As Object
    Dim UserName As String
    Dim PI_name As Variant
    ''Dim i As Integer
    
    ''i = 0
    
    ThisWorkbook.Sheets("EMAIL_LIST").Activate
    
    ' Locate the header
      Set NameRange = Cells.Find(What:="PI_Name", LookIn:=xlValues, MatchCase:=False)
     
    ' If the header was not found, exit the macro with a message
      If NameRange Is Nothing Then MsgBox "Header 'PI_name' Not Found.": Exit Sub
    
    ' Change the NameRange to point to the first name in the list
      Set NameRange = NameRange.Offset(1, 0)
    
    ' Find the last name in the list
      Set RngEnd = Cells(Rows.Count, NameRange.Column).End(xlUp)
    
    ' If there are no names, exit the macro with a message
      If RngEnd.Row < NameRange.Row Then MsgBox "No Names were found.": Exit Sub
    
    ' Change the NameRange to include all the names in the list
      Set NameRange = Range(NameRange, RngEnd)
    
    ''ActiveCell.Offset(1, 0).Activate
    
    ''PI_name = ActiveCell.Value
    
    ''ActiveWorkbook.Names.Add Name:="Bookmark", RefersTo:=ActiveCell
    
    ' Step through each name and send an email
      For Each PI_name In NameRange
        
        Set Notes = CreateObject("Notes.NotesSession")
        UserName = Notes.UserName
        
        Set WorkSpace = CreateObject("Notes.NotesUIWorkspace")
        
        Call WorkSpace.ComposeDocument(, , "Memo")
        
        Set UIdoc = WorkSpace.CurrentDocument
        
        ' Recipient = ThisWorkbook.Sheets("EMAIL_LIST").Range("A2").Value
          Recipient = PI_name.Value
        
          Call UIdoc.FieldSetText("EnterSendTo", Recipient)
        
          Subject1 = "Testing from Macro"
          Call UIdoc.FieldSetText("Subject", Subject1)
        
          Call UIdoc.GotoField("Body")
          Body1 = "Hi This is a test"
          Call UIdoc.InsertText(Body1)
        
        ' Insert some carriage returns at the end of the email
          Call UIdoc.InsertText(vbCrLf & vbCrLf)
        
          UIdoc.send (True)
          UIdoc.Close
         
        Set UIdoc = Nothing: Set WorkSpace = Nothing: Set Notes = Nothing
        
        ''Application.Goto Reference:="Bookmark"
        ''i = i + 1
        ''PI_name = ActiveCell.Offset(i, 0).Value
        
      Next PI_name
    
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    10-11-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Exception Handling of invalid email address

    Thanks Leith,

    I am sure your enhancements are very apt and I have learnt from it.

    I do have a question though.

    In the "for" loop, once the email is ready to be sent, what happens is that if the email address is incorrect, the macro will hang, because there is a lotus notes error (saying something like the email address is not found or something similar to that). How can I trap that exception and handle it appropriately.

    Ideally, what I would want is to open a new sheet and copy that row with incorrect address onto a new sheet in the same workbook, and continue with the next PI_Name

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Exception Handling of invalid email address

    Here's a UDF that tests for a valid email address
    Option Explicit
    
    Public Function ValidEmail(pAddress As String) As Boolean
         '-----------------------------------------------------------------
        Dim oRegEx As Object
        Set oRegEx = CreateObject("VBScript.RegExp")
        With oRegEx
            .Pattern = "^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$"
            ValidEmail = .Test(pAddress)
        End With
        Set oRegEx = Nothing
    End Function
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  7. #7
    Registered User
    Join Date
    10-11-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Exception Handling of invalid email address

    Can you explain how this works and how should I incorporate it into my code

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1