【转载】VBA从字符串中提取电子邮件的函数
时 间:2020-06-23 08:49:16
作 者:金宇 ID:43 城市:江阴
摘 要:通过正则表达式提取电子邮件。
正 文:
主要通过正则表达式来提取字符串中的电子邮件,代码如下:
'--------------------------------------------------------------------------------------- ' Procedure : ExtractEmailAddresses ' Author : Daniel Pineault, CARDA Consultants Inc. ' Website : http://www.cardaconsultants.com ' Purpose : Extract e-mail addresses from a supplied string ' Notes : None ' Copyright : The following is release as Attribution-ShareAlike 4.0 International ' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/ ' Req'd Refs: Uses Late Binding, so none required ' ' Input Variables: ' ~~~~~~~~~~~~~~~~ ' sInput : String to parse/extract e-mail addresses from ' ' Usage: ' ~~~~~~ ' See TestMe Sub ' ' Revision History: ' Rev Date(yyyy/mm/dd) Description ' ************************************************************************************** ' 1 2020-05-13 Initial Release, Forum Help ' 2 2020-05-17 Added a more advanced pattern '--------------------------------------------------------------------------------------- Public Function ExtractEmailAddresses(ByVal sInput As Variant) As Variant On Error GoTo Error_Handler Dim oregEx As Object Dim oMatches As Object Dim oMatch As Object Dim sEmail As String If Not IsNull(sInput) Then Set oregEx = CreateObject("vbscript.regexp") With oregEx 'Basic pattern '.Pattern = "([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)" 'More advanced pattern that allow accented characters .Pattern = "([a-zA-ZF0-9\u00C0-\u017F._-]+@[a-zA-Z0-9\u00C0-\u017F._-]+\.[a-zA-Z0-9\u00C0-\u017F_-]+)" .Global = True .IgnoreCase = True .MultiLine = True Set oMatches = .Execute(sInput) End With For Each oMatch In oMatches sEmail = oMatch.Value & "," & sEmail Next oMatch If Right(sEmail, 1) = "," Then sEmail = Left(sEmail, Len(sEmail) - 1) ExtractEmailAddresses = Split(sEmail, ",") 'Return an array of email addresses extracted from sInput Else ExtractEmailAddresses = Null End If Error_Handler_Exit: On Error Resume Next If Not oMatch Is Nothing Then Set oMatch = Nothing If Not oMatches Is Nothing Then Set oMatches = Nothing If Not oregEx Is Nothing Then Set oregEx = Nothing Exit Function Error_Handler: MsgBox "The following error has occured" & vbCrLf & vbCrLf & _ "Error Number: " & Err.Number & vbCrLf & _ "Error Source: ExtractEmailAddresses" & vbCrLf & _ "Error Description: " & Err.Description & _ Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _ , vbOKOnly + vbCritical, "An Error has Occured!" Resume Error_Handler_Exit End Function
附 件:
图 示:
Access软件网QQ交流群 (群号:54525238) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- Access对子窗体数据进行批...(10.30)
- 最精简的组合框行来源数据快速输...(10.25)
- Access仿平台的多值选择器...(10.24)
- 【Access日期区间段查询】...(10.22)
- 【Access源码示例】VBA...(10.12)
- Access累乘示例,Acce...(10.09)
- 数值8.88,把整数8去掉,转...(10.08)
- 【Access自定义函数】一个...(09.30)
- 【Access选项卡示例】Ac...(09.09)
学习心得
最新文章
- Access系统自带的日期选择器不...(11.08)
- 分享一下Access工程中的acw...(11.07)
- Access快速开发平台--让有权...(11.04)
- Access快速开发平台--审批选...(11.01)
- ACCESS两张表先各自排序,然后...(10.31)
- Access对子窗体数据进行批量+...(10.30)
- SqlServer中如何用SQL命...(10.29)
- Access报表中的分组功能用代码...(10.28)
- 用Access计算库存结余的一个方...(10.26)
- 最精简的组合框行来源数据快速输入(...(10.25)