kerrypnx
11/5/2018 - 3:13 AM

检测邮箱的个数是否正确

revise 11-5

Sub detectEmailCount()
Selection.HomeKey wdStory
With Selection.Find
    .ClearFormatting
    .Text = "Received: "
    .MatchWildcards = True
    .Replacement.Text = ""
    .Execute
End With
Dim myrange As Range
Set myrange = ActiveDocument.Range(ActiveDocument.Paragraphs(3).Range.End, Selection.Range.Start)
myrange.Select
If FunctionGroup.emailCount(myrange.Text) <> FunctionGroup.authorCount(ActiveDocument.Paragraphs(3).Range.Text) Then
    myrange.comments.Add myrange, "The number of authors does not match the number of email"
End If
End Sub

Function authorCount(str As String) As Integer
    If InStr(str, ", ") = 0 And InStr(str, " and ") = 0 Then
        authorCount = 1
    End If
    
    If InStr(str, ", ") = 0 And InStr(str, " and ") > 0 Then
        authorCount = 2
    End If
    
    If InStr(str, ", ") > 0 Then
        If InStr(str, " and ") = 0 Then
            ActiveDocument.Paragraphs(3).Range.HighlightColorIndex = wdRed
            ActiveDocument.Paragraphs(3).Range.comments.Add ActiveDocument.Paragraphs(3).Range, ""
        Else
            Dim reg As New RegExp
            Dim matches
            With reg
                .Global = True
                .Pattern = "\, "
                Set matches = reg.Execute(str)
                authorCount = matches.count + 2
            End With
        End If

    End If
End Function


Function emailCount(str As String) As Integer
Dim matches
Dim atCount, orCount   As Integer
Dim reg As New RegExp
With reg
    .Global = 1
    .Pattern = "\@"
    Set matches = .Execute(str)
End With
atCount = matches.count

With reg
    .Global = True
    .Pattern = " or [^ ]+\@"
    Set matches = .Execute(str)
End With

orCount = matches.count
emailCount = atCount - orCount
End Function