'---------------------------------------------------------------------------------------
' Procedure : Emailfinder
' Author : Jack
' Date : 18/02/2014
' Purpose : This will get valid email addresses from a string according to link.
' Found "best regex pattern for email validation" at
' http://blog.trojanhunter.com/2012/09/26/the-best-regex-to-validate-an-email-address
' Pattern: [-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4}
'
' This routine will return multiple email addresses when there are multiples in the string.
'
'' ---------------------------------------
'*** Requires a reference to the Microsoft VBScript Regular Expressions library
' _______________________________________
'---------------------------------------------------------------------------------------
'
Function Emailfinder(t As String) As String
Dim MyRE As Object
10 Set MyRE = New Regexp
Dim MyMatches As MatchCollection
Dim MyResult As String
'set the email pattern
20 On Error GoTo Emailfinder_Error
30 Emailfinder = "" 'set to empty string
40 MyRE.Pattern = "[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4}"
50 MyRE.Global = True
60 MyRE.IgnoreCase = True
70 Set MyMatches = MyRE.Execute(t)
80 If MyMatches.Count > 0 Then
90 For Each MyMatch In MyMatches
100 MyResult = MyResult & MyMatch.Value & vbCrLf
110 Next
120 Emailfinder = MyResult ' one or more valid email addresses
130 Else
140 Emailfinder = "" 'empty string
150 End If
160 On Error GoTo 0
170 Exit Function
Emailfinder_Error:
180 MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure Emailfinder of Module Module1"
End Function