我想知道是否有人在 Microsoft Outlook 中遇到过包含提及(例如 @Tony 或 @Jodie)的正则表达式验证问题。当电子邮件中没有提及时,以下代码可以正常工作。但是,当我在电子邮件中提及某人时,它会验证电子邮件正文中单词内的随机字母,我不确定为什么。
我唯一的解决方案就是在电子邮件中不提及任何人,而只是将他们添加到收件人框中。如果其他人也遇到过此问题,请告诉我:
Sub ValidateAndFormatText()
Dim olApp As Object
Dim olInspector As Object
Dim olMailItem As Object
Dim olRange As Object
Dim re As Object
Dim signatureStart As Long
Dim signatureEnd As Long
Dim textToValidate As String
Dim match As Object
' Initialize Outlook objects
Set olApp = CreateObject("Outlook.Application")
Set olInspector = olApp.ActiveInspector
Set olMailItem = olInspector.currentItem
Set olRange = olMailItem.GetInspector.wordEditor.Range
' Define your signature text
Dim signatureText As String
signatureText = "Best,"
' Find the position of the signature
signatureStart = InStr(1, olRange.text, signatureText, vbTextCompare)
signatureEnd = signatureStart + Len(signatureText)
' Extract the text above the signature
textToValidate = Left(olRange.text, signatureStart - 1)
' Initialize the regular expression
Set re = CreateObject("VBScript.RegExp")
re.Global = True
re.IgnoreCase = True
' Validate and format monetary values
re.pattern = "\$[0-9]{1,3}(([\.?,?][0-9]{1,3}){1,})?\b"
For Each match In re.Execute(textToValidate)
olRange.Start = match.FirstIndex
olRange.End = match.FirstIndex + match.Length
olRange.Font.Bold = True
olRange.Font.Color = RGB(8, 30, 54) ' Navy blue
Next match
' Validate and format monetary values
re.pattern = "\$[0-9]{1,3}[KM]\b"
For Each match In re.Execute(textToValidate)
olRange.Start = match.FirstIndex
olRange.End = match.FirstIndex + match.Length
olRange.Font.Bold = True
olRange.Font.Color = RGB(8, 30, 54) ' Navy blue
Next match
' Validate and format time values (e.g., 10:30 AM)
re.pattern = "\b\d{1,2}:\d{2}(?:\s?[AaPp](\.?)[Mm]\1)?"
For Each match In re.Execute(textToValidate)
olRange.Start = match.FirstIndex
olRange.End = match.FirstIndex + match.Length
olRange.Font.Bold = True
olRange.Font.Color = RGB(8, 30, 54) ' Navy blue
Next match
' Validate any digits with or without percentage signs
re.pattern = "[0-9]{1,3}(.[0-9]{1,2})?\%"
For Each match In re.Execute(textToValidate)
olRange.Start = match.FirstIndex
olRange.End = match.FirstIndex + match.Length
olRange.Font.Bold = True
olRange.Font.Color = RGB(8, 30, 54) ' Navy blue
Next match
' Number Long
re.pattern = "\b(?:two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety)\b(\s\([0-9]{1,}\))?"
For Each match In re.Execute(textToValidate)
olRange.Start = match.FirstIndex
olRange.End = match.FirstIndex + match.Length
olRange.Font.Bold = True
olRange.Font.Color = RGB(8, 30, 54) ' Navy blue
Next match
' Weekday
re.pattern = "\b(Happy)? (Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)\b"
For Each match In re.Execute(textToValidate)
olRange.Start = match.FirstIndex
olRange.End = match.FirstIndex + match.Length
olRange.Font.Bold = True
olRange.Font.Color = RGB(8, 30, 54) ' Navy blue
Next match
' Random Codes etc
re.pattern = "\b(([0-9]{1,}) psi|([0-9]{1,}) PSI|NFPA (25)|([0-9]{1,}) GPM|([0-9]{1,}) mph|([0-9]{1,}) MPH)\b"
For Each match In re.Execute(textToValidate)
olRange.Start = match.FirstIndex
olRange.End = match.FirstIndex + match.Length
olRange.Font.Bold = True
olRange.Font.Color = RGB(8, 30, 54) ' Navy blue
Next match
' Civil Code
re.pattern = "\bCivil Code \d{4}(\(?[a-zA-Z]\)?)?"
For Each match In re.Execute(textToValidate)
olRange.Start = match.FirstIndex
olRange.End = match.FirstIndex + match.Length
olRange.Font.Bold = True
olRange.Font.Color = RGB(8, 30, 54) ' Navy blue
Next match
' Palos Verdes Bay Club Building and Units
re.pattern = "\b[0-9]{1,2}-([A-H]|[0-9]{3})\b"
For Each match In re.Execute(textToValidate)
olRange.Start = match.FirstIndex
olRange.End = match.FirstIndex + match.Length
olRange.Font.Bold = True
olRange.Font.Color = RGB(8, 30, 54) ' Navy blue
Next match
' Number (00)
re.pattern = "\b(?:two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety)(\s\(\d+\))?\b"
For Each match In re.Execute(textToValidate)
olRange.Start = match.FirstIndex
olRange.End = match.FirstIndex + match.Length
olRange.Font.Bold = True
olRange.Font.Color = RGB(8, 30, 54) ' Navy blue
Next match
' 28-day comment period
re.pattern = "\b28-day\s(comment period)?"
For Each match In re.Execute(textToValidate)
olRange.Start = match.FirstIndex
olRange.End = match.FirstIndex + match.Length
olRange.Font.Bold = True
olRange.Font.Color = RGB(8, 30, 54) ' Navy blue
Next match
' Validate and format long date values (e.g., Sunday, May 9, 2024)
re.pattern = "\b((?:Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday),\s)?(?:January|February|March|April|May|June|July|August|September|October|November|December) (\d{1,2}(,)?) \d{4}\b"
For Each match In re.Execute(textToValidate)
olRange.Start = match.FirstIndex
olRange.End = match.FirstIndex + match.Length
olRange.Font.Bold = True
olRange.Font.Color = RGB(8, 30, 54) ' Navy blue
Next match
' Validate and format long date values (e.g., 02/14/2024)
re.pattern = "(?:January|February|March|April|May\b|June|July|August|September|October|November|December)(\s\d{1,4})?(,?\s\d{4})?"
For Each match In re.Execute(textToValidate)
olRange.Start = match.FirstIndex
olRange.End = match.FirstIndex + match.Length
olRange.Font.Bold = True
olRange.Font.Color = RGB(8, 30, 54) ' Navy blue
Next match
' [#XN] number
re.pattern = "\[#XN[0-9]{7}\]"
For Each match In re.Execute(textToValidate)
olRange.Start = match.FirstIndex
olRange.End = match.FirstIndex + match.Length
olRange.Font.Bold = True
olRange.Font.Color = RGB(8, 30, 54) ' Navy blue
Next match
' Validate and format date values (e.g., 02/14/2024)
re.pattern = "\b[0-9]{1,2}[/-][0-9]{1,2}([/-][0-9]{2,4})?\b"
For Each match In re.Execute(textToValidate)
olRange.Start = match.FirstIndex
olRange.End = match.FirstIndex + match.Length
olRange.Font.Bold = True
olRange.Font.Color = RGB(8, 30, 54) ' Navy blue
Next match
[![enter image description here][1]][1]
End Sub