Highlight found RegEx pattern

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
 

shrydvd

New Member
Outlook version
Outlook 2019 64-bit
Email Account
IMAP
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!
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 
Thread starter Similar threads Forum Replies Date
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
F I cannot highlight text in Microsoft Outlook 2010 e-mails, and copy/paste to the body of another e-m Using Outlook 3
S I can send e-mails but not receive any. When I highlight the server box I get this....please help!__ Using Outlook 1
B Highlight Unopened Emails in Inbox Using Outlook 1
X Highlight block of text in an outlook mail using c#2008 vsto Outlook VBA and Custom Forms 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
D Operation failed. An object could not be found. Using Outlook 2
S Help: Your digital id name cannot be found by the underlying security system. Using Outlook 3
T Can receive but can not send get error: The attemped operation failed, an object can't be found. Using Outlook 3
A Emails stuck in Outbox/new messages can't be found. Using Outlook 2
A "Item could not be found" error when attempting to send Outlook 2007 calendar event update Using Outlook 2
C Completely lost my 'Favorites' folder - nowhere to be found Outlook 2007 Using Outlook 3
J file c outlook cannot be found Using Outlook 2
L some related messages not found when replying from iPhone Using Outlook 2
M Outlook 2010 - The attempted operation failed. An object could not be found. Using Outlook 3
M No data file found and when I try to enter a personal folder it wont let me. Using Outlook 15
Similar threads


















































Top