0

I was wondering if anyone came across the issue with Regex validation in Microsoft Outlook when including mentions (i.e.,@Tony or @Jodie). The below code works fine when there is no mentions in the email. However, when I mention someone in the email it will validate random letters within words in the email body and I'm not sure why.

My only solution is to just not mention anyone in the email and to just add them to the recipient box. Let me know if anyone else has dealt with this issue:

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
4
  • Is the entirety of that code necessary for the MRE? Your screenshot wasn’t uploaded
    – Ramhound
    Commented Mar 14 at 0:52
  • @mentions are more than just text, so it may help you to see here for a little more info on what their actual value-expression is: stackoverflow.com/questions/58214117/… . just a WAG. Commented Mar 14 at 3:28
  • Just checking in to see if above information was helpful. If you have any further updates on this issue, please feel free to post back.
    – Soki
    Commented Mar 14 at 7:55
  • Hi @Soki, it helps with understanding that the email is probably looking to validate letters/numbers in the mention ID, but I am still unsure why any of my Regex patterns in the above would be validating that ID if the patterns are narrow and for specific purposes. Since I can't tell what the ID is off hand it is hard to account for this and wanted to see if anyone has any insight on a workaround instead of not including mentions. Commented Mar 14 at 18:34

0

You must log in to answer this question.

Browse other questions tagged .