楼主 chenlifeng |
通过反复测试,下面三个自定义函数,能把当前WORD中的手机、固话、邮箱地址,全部匹配出来,特作分享,谢谢!- Function 调用固话() As String
- Dim RegExp As Object, str, mMatch
- str = ActiveDocument.range.text
- Set RegExp = CreateObject("vbscript.regexp")
- With RegExp
- .Global = True
- .IgnoreCase = True
- .Pattern = "((0[0-9]{2,3})?(\-|\_|[ ]{0,})?([2-9][0-9]{3})+([ ]{0,})?([0-9]{3,4})+((\-|_|转| {0,}|分机)[0-9]{1,4})?)" '"(0[0-9]{2,3})?(-|_|[ ]{0,})?([2-9][0-9]{3})+([ ]{0,})?([0-9]{3,4})+((\-|_|转| {0,}|分机)[0-9]{1,4})?"
- End With
- If RegExp.test(str) Then
- For Each mMatch In RegExp.Execute(str)
- If Len(GetNum(mMatch.Value, 1)) = 7 Or Len(GetNum(mMatch.Value, 1)) = 8 Or Len(GetNum(mMatch.Value, 1)) = 11 Or Len(GetNum(mMatch.Value, 1)) = 12 Or Len(GetNum(mMatch.Value, 1)) = 13 Or Len(GetNum(mMatch.Value, 1)) = 14 Or Len(GetNum(mMatch.Value, 1)) = 15 Or Len(GetNum(mMatch.Value, 1)) = 16 Then
- With Selection.Find
- .text = mMatch
- .Wrap = wdFindContinue
- End With
- Selection.Find.Execute
- Selection.MoveLeft Unit:=wdCharacter, Count:=1
- Selection.MoveLeft Unit:=wdCharacter, Count:=4, Extend:=wdExtend
- Dim 前范围, 后范围, Reg As Object
- 前范围 = Selection
- Selection.MoveRight Unit:=wdCharacter, Count:=1
- Selection.MoveRight Unit:=wdCharacter, Count:=Len(mMatch)
- Selection.MoveRight Unit:=wdCharacter, Count:=4, Extend:=wdExtend
- 后范围 = Selection
- Set Reg = CreateObject("vbscript.regexp")
- With Reg
- .Global = True
- .IgnoreCase = False
- .MultiLine = True
- .Pattern = "固话|固|电话|座机|座|家庭|庭"
- End With
- If Reg.test(前范围 & 后范围) Then
- 调用固话 = Replace(调用固话, " ", "") & ";" & mMatch.Value
- Else
- If (Len(GetNum(mMatch.Value, 1)) = 16 Or Len(GetNum(mMatch.Value, 1)) = 15 Or Len(GetNum(mMatch.Value, 1)) = 12 Or Len(GetNum(mMatch.Value, 1)) = 11) And Mid(GetNum(mMatch.Value, 1), 1, 1) = 0 And InStr(前范围, "证") = 0 And InStr(前范围, "编") = 0 And InStr(后范围, "(证") = 0 And InStr(后范围, "(编") = 0 Then
- 调用固话 = Replace(调用固话, " ", "") & ";" & mMatch.Value
- ElseIf (Len(GetNum(mMatch.Value, 1)) = 8 Or Len(GetNum(mMatch.Value, 1)) = 7) And Mid(GetNum(mMatch.Value, 1), 1, 1) > 1 And InStr(前范围, "证") = 0 And InStr(前范围, "编") = 0 And InStr(后范围, "(证") = 0 And InStr(后范围, "(编") = 0 Then
- 调用固话 = Replace(调用固话, " ", "") & ";" & mMatch.Value
- End If
- End If
- End If
- Next mMatch
- Else
- 调用固话 = "没有固定电话,或输错位数与空位置错"
- End If
- Selection.HomeKey Unit:=wdStory '把光标移动的文档首
- If Mid(调用固话, 1, 1) = ";" Then
- 调用固话 = "固话:" & Mid(调用固话, 2, Len(调用固话) - 1)
- Else: 调用固话 = IIf(调用固话 = "", "固话:可能不存在", "固话:" & 调用固话)
- End If
- End Function
- Function 调用手机() As String
- Dim RegExp As Object, str, mMatch
- str = ActiveDocument.range.text
- Set RegExp = CreateObject("vbscript.regexp")
- With RegExp
- .Global = True
- .IgnoreCase = True
- .Pattern = "(((\+86)|(86))?([ ]{0,})?(13|14|15|18)+(\d{1})+([ ]{0,})?(\d{4})+([ ]{0,})?(\d{4})+|((\+86)|(86))?([ ]{0,})?(13|14|15|18)+(\d{2})+([ ]{0,})?(\d{4})+([ ]{0,})?(\d{3})+)"
- End With
- If RegExp.test(str) Then
- For Each mMatch In RegExp.Execute(str)
- If Len(GetNum(mMatch.Value, 1)) = 11 Or Len(GetNum(mMatch.Value, 1)) = 13 Or Len(GetNum(mMatch.Value, 1)) = 14 Then
- With Selection.Find
- .text = mMatch
- .Wrap = wdFindContinue
- End With
- Selection.Find.Execute
- Selection.MoveLeft Unit:=wdCharacter, Count:=1
- Selection.MoveLeft Unit:=wdCharacter, Count:=4, Extend:=wdExtend
- Dim 前范围, 后范围, Reg As Object
- 前范围 = Selection
- Selection.MoveRight Unit:=wdCharacter, Count:=1
- Selection.MoveRight Unit:=wdCharacter, Count:=Len(mMatch)
- Selection.MoveRight Unit:=wdCharacter, Count:=4, Extend:=wdExtend
- 后范围 = Selection
- Set Reg = CreateObject("vbscript.regexp")
- With Reg
- .Global = True
- .IgnoreCase = False
- .MultiLine = True
- .Pattern = "手机|手|ell|hon|all|ber"
- End With
- If Reg.test(前范围 & 后范围) Then
- 调用手机 = Replace(调用手机, " ", "") & ";" & mMatch.Value
- Else
- If Len(GetNum(mMatch.Value, 1)) = 14 And Mid(GetNum(mMatch.Value, 1), 1, 3) = "+86" And InStr(前范围, "证") = 0 And InStr(前范围, "编") = 0 And InStr(后范围, "(证") = 0 And InStr(后范围, "(编") = 0 Then
- 调用手机 = Replace(调用手机, " ", "") & ";" & mMatch.Value
- ElseIf Len(GetNum(mMatch.Value, 1)) = 13 And Mid(GetNum(mMatch.Value, 1), 1, 3) = "86" And InStr(前范围, "证") = 0 And InStr(前范围, "编") = 0 And InStr(后范围, "(证") = 0 And InStr(后范围, "(编") = 0 Then
- 调用手机 = Replace(调用手机, " ", "") & ";" & mMatch.Value
- ElseIf Len(GetNum(mMatch.Value, 1)) = 11 And InStr(前范围, "证") = 0 And InStr(前范围, "编") = 0 And InStr(后范围, "(证") = 0 And InStr(后范围, "(编") = 0 Then
- 调用手机 = Replace(调用手机, " ", "") & ";" & mMatch.Value
- End If
- End If
- End If
- Next mMatch
- Else
- 调用手机 = "没有手机号码,或输错号码与位数"
- End If
- Selection.HomeKey Unit:=wdStory '把光标移动的文档首
- If Mid(调用手机, 1, 1) = ";" Then
- 调用手机 = "手机:" & Mid(调用手机, 2, Len(调用手机) - 1)
- Else: 调用手机 = IIf(调用手机 = "", "手机:可能不存在", "固话:" & 调用手机)
- End If
- End Function
- Function 调用邮箱() As String
- Dim RegExp As Object, str, mMatch
- str = ActiveDocument.range.text
- Set RegExp = CreateObject("vbscript.regexp")
- With RegExp
- .Global = True
- .IgnoreCase = True
- .Pattern = "(([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?))"
- End With
- If RegExp.test(str) Then
- For Each mMatch In RegExp.Execute(str)
- With Selection.Find
- .text = mMatch
- .Wrap = wdFindContinue
- End With
- Selection.Find.Execute
- Selection.MoveLeft Unit:=wdCharacter, Count:=1
- Selection.MoveLeft Unit:=wdCharacter, Count:=4, Extend:=wdExtend
- Dim 前范围, 后范围, Reg As Object
- 前范围 = Selection
- Selection.MoveRight Unit:=wdCharacter, Count:=1
- Selection.MoveRight Unit:=wdCharacter, Count:=Len(mMatch)
- Selection.MoveRight Unit:=wdCharacter, Count:=4, Extend:=wdExtend
- 后范围 = Selection
- Set Reg = CreateObject("vbscript.regexp")
- With Reg
- .Global = True
- .IgnoreCase = False
- .MultiLine = True
- .Pattern = "邮箱|电子|邮件|mail|邮|地址|址"
- End With
- If Reg.test(前范围 & 后范围) Then
- 调用邮箱 = 调用邮箱 & ";" & mMatch.Value
- Else
- If InStr(前范围, "网络") = 0 And InStr(前范围, "网页") = 0 And InStr(后范围, "(网络") = 0 And InStr(后范围, "(网页") = 0 Then 调用邮箱 = 调用邮箱 & ";" & mMatch.Value
- End If
- Next mMatch
- Else
- 调用邮箱 = "没有电子邮箱,或漏输入了“@”"
- End If
- Selection.HomeKey Unit:=wdStory '把光标移动的文档首
- If Mid(调用邮箱, 1, 1) = ";" Then
- 调用邮箱 = "邮箱:" & Mid(调用邮箱, 2, Len(调用邮箱) - 1)
- Else: 调用邮箱 = IIf(调用邮箱 = "", "邮箱:可能不存在", "邮箱:" & 调用邮箱)
- End If
- End Function
|