Highlight found RegEx pattern

Status
Not open for further replies.

shrydvd

New Member
Outlook version
Outlook 2019 64-bit
Email Account
IMAP
The following code will warn the user that they may have sensitive information in the body of their email. However, sometimes that body may be quite long and it's not easy to see what the code is referring to when it gives the message. I am wanting to add to this code something that will highlight the matched strings in the body. I would imagine it will be something like "for each matched string, .highlight=true? or something like that.
Thank you,
Code:
 Dim myMailToSend As MailItem
    Dim re As Object
    Const sPat As String = "\b\d{3}[\D]\d{2}[\D]\d{4}\b|\b\d{9}|\b\d{2}[\D]\d{7}\b"

    Set myMailToSend = Item
    Set re = CreateObject("vbscript.regexp")
    re.Pattern = sPat
    s = myMailToSend.Body & " " & myMailToSend.Subject
    If re.Test(s) = True Then
        strMsg = "This email appears to contain sensitive information." & _
                                    "Do you still want to send it?"

        nResponse = MsgBox(strMsg, vbExclamation + vbYesNo + vbDefaultButton2, _
                                    "Check Sensitive Information")
        If nResponse = vbYes Then
            Cancel = False
        Else
            Cancel = True
            Exit Sub
        End If
    End If
 
As far as I know, there is not a way to highlight, save for replacing the text with formatted text. Maybe if the user says no, bring it up in the office find dialog?
I was afraid you would say that. That's OK though; It would have just been a "bonus" on the script I was doing, not a necessity.
Thank you for replying!
 
What you could do is create a list of found entries (use re.global = true) and list them in a dialog box. What would be better is to find maybe 5 characters before and after -

I grabbed the code from here - Use RegEx to extract text from an Outlook email message - and did not test it so the code may need tweaked, but something like this could work.
Code:
 With Reg1       
 .Pattern = "(.{5}(\b\d{3}[\D]\d{2}[\D]\d{4}\b|\b\d{9}|\b\d{2}[\D]\d{7}\b).{5})"       
 .Global = True    
End With

If Reg1.test(olMail.Body) Then
    
        Set M1 = Reg1.Execute(olMail.Body)
        For Each M In M1
               strShare = M.SubMatches(0) 
               strMatch = M.SubMatches(1)  
        Next
    End If

If strMatch <> "" Then 

strMsg = "This email appears to contain sensitive information: " & strShare & _
                                    "Do you still want to send it?"
        nResponse = MsgBox(strMsg, vbExclamation + vbYesNo + vbDefaultButton2, _
"Check Sensitive Information")
If nResponse = vbYes Then
Cancel = False
Else
Cancel = True
Exit Sub
        End If
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
P Color Code or highlight folders in Outlook 2016 Using Outlook 2
Y VBA Macro to highlight some Keyword in mail body? Outlook VBA and Custom Forms 3
J Find and Highlight text in outlook Outlook VBA and Custom Forms 4
F VBA script to highlight specific words Outlook VBA and Custom Forms 1
M Automatically highlight unread mail in Favorites inbox(s) (Blue number) Using Outlook 1
M Editing tasks: highlight & copy/cut NOT working in Calendar entries Using Outlook 4
J Is it possible to highlight a folder in Outlook 2007? Using Outlook 2
Rupert Dragwater How to highlight lists of same e-mail Using Outlook 2
P Addin / macro / rule to highlight email addresses outside company in To: field Using Outlook 3
X Highlight block of text in an outlook mail using c#2008 vsto Outlook VBA and Custom Forms 1
V Outlook Error The Attempted operation Failed. An Object Could Not be found Outlook VBA and Custom Forms 0
B Outlook 213 keeps crashing. I just found out there are DMP files. How can I tell Why? Using Outlook 2
D Forwarding email based on the attachment file type and specific text found on the attachment file name Outlook VBA and Custom Forms 1
C Not sync folders not found after MS Outlook 365 update Using Outlook 1
rubaboo The vew cannot be found Outlook VBA and Custom Forms 0
J Outlook 2016 After a search in all mailboxes, where is each message that was found? Using Outlook 6
noshairwan Registry cannot be found, Outlook Security Using Outlook 2
M Sent mail not showing in Sent Items folder; but they can be found with Search Using Outlook 3
A VBAProject.OTM Not Found Outlook VBA and Custom Forms 2
T outlook.com opens with "item not found" Using Outlook 1
vodkasoda Object could not be found Error in Outlook 2007 Outlook VBA and Custom Forms 5
P Automate Outlook Start - No Active Explorer Object Found Using Outlook 10
K How to access emails found to be located in "Top of Outlook data file"? Using Outlook 3
G Calendar folder cannot be found (tasks folder cannot be found) Using Outlook 2
T Desktop alerts show but no emails found Using Outlook 1
R Inbox search, current folder, it's in there-but not found. Using Outlook 2
K After Carbonite restore pst files are found in a weird location Using Outlook 2
R Exchange - Locating a user that has log files but is not found using powershell Exchange Server Administration 1
G Script in rule to send to multiple emails found in message bo Outlook VBA and Custom Forms 11
J An attempt operation failed. An object could not be found at line5. Outlook VBA and Custom Forms 2
O "Cannot create message because a data file...could not be found" Using Outlook 1
R Clip art search alway no result found in Outlook 2010 Using Outlook 1
C Odd error message not found in Google search Using Outlook 1
M "No database found" but successful connection to SQL server.What am I missing? BCM (Business Contact Manager) 5
E Outlook PST files disappear, only found temp file HELP: Using Outlook 1
R Lost Outlook 2007 message found in OWA Using Outlook 4
A 5.7.1 Recipient not authorized, your IP has been found on a block list Using Outlook 1
O Lost attachment found but hidden in msg! Help!! Using Outlook 1
D Found and Lost Outlook Add-ins Using Outlook 0
C Anyone found a way to customize search? BCM (Business Contact Manager) 5
C User defined field not found in any views prints. Cannot locate to delete. Using Outlook 2
K Outlook insert clip art, no results found...address book contacts only show up Using Outlook 5
B Task List in To-Do Bar 'The operation failed. An object could not be found' Using Outlook 3
M The macro name.ThisOutlookSession.DisplayMyForm cannot be found. Outlook VBA and Custom Forms 2
M Cannot Send - "Operation failed. An object cannot be found" BCM (Business Contact Manager) 4
A path of documents can't be found BCM (Business Contact Manager) 1
O VBA - Regex - remove double line spacing Outlook VBA and Custom Forms 1
J Outlook 2019 Regex email addresses from body Outlook VBA and Custom Forms 6
A How to assign the value returned by the regex execute function to a variable? Using Outlook 1
G regex to search for link in attachments? Outlook VBA and Custom Forms 2

Similar threads

Back
Top