Microsoft Outlook Add-In: Contextual Regex for Links

joostwmd 10 Reputation points
2024-12-15T19:52:54.6366667+00:00

I want to develop a Microsoft Outlook add-in that highlights links in Emails. I found this example for a add in that highlights order numbers and displays a popup when clicking on it. The Code "responsible" for that is this regex in the manifest:

<Rule xsi:type="RuleCollection" Mode="And">

<Rule xsi:type="ItemIs" ItemType="Message" />

<Rule xsi:type="ItemHasRegularExpressionMatch" RegExName="OrderNumber" RegExValue="CO-\d{9}" PropertyName="BodyAsPlaintext"/>

</Rule>

I basically want the same for any link in an email. I thought I could just use a regex for a link, bit somehow it is not working. Here is my current code:

<Rule xsi:type="RuleCollection" Mode="And">

<Rule xsi:type="ItemIs" ItemType="Message" />

<Rule xsi:type="ItemHasRegularExpressionMatch" RegExName="Link" RegExValue="(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})" PropertyName="BodyAsPlaintext"/>

</Rule>

My goal is to prevent the default behaviour which is opening the link and displaying the popup instead, like in the example:

Screenshot 2024-12-15 at 20.52.04

Has anyone figured this out?

Microsoft 365 and Office Development Other
Outlook Windows Classic Outlook for Windows For business
Microsoft 365 and Office Install, redeem, activate For business Windows
{count} votes

1 answer

Sort by: Most helpful
  1. Digvijay Chauhan 1 Reputation point
    2024-12-15T23:06:10.4933333+00:00

    Hi joostwmd,

    Could you get the example to run locally before you started to play around with your regular expression?

    It is actually two parts :

    In the file contoso-order-number-manifest.xml line 52-56

    <Rule xsi:type="RuleCollection" Mode="And">
        <Rule xsi:type="ItemIs" ItemType="Message" FormType="Read" />
        <Rule xsi:type="ItemHasRegularExpressionMatch" RegExName="9Digits" RegExValue="\d{9}" 			PropertyName="BodyAsPlaintext"/>  
    </Rule>   
    <DisableEntityHighlighting>false</DisableEntityHighlighting>
    
    

    Notice 9Digits - it must match so it goes to second one: line 74-77

     <Rule xsi:type="RuleCollection" Mode="And">
                    <Rule xsi:type="ItemIs" ItemType="Message" />
                    <Rule xsi:type="ItemHasRegularExpressionMatch" RegExName="OrderNumber" RegExValue="CO-\d{9}" PropertyName="BodyAsPlaintext"/>              
    </Rule>
    
    

    So i guess if you just put the rule either in top block or make it pass on to second rule it should work!

    Hope that helps!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.