• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How to open a specific link automatically with outlook

Outlook version
Outlook 2016 64 bit
Email Account
POP3
#1
I wonder if anybody could help me. I would like to automatically open a specific "accept" link (which is not the first link on the email, it's the third) and leave the other links alone (especially the "reject" link!).

I have tried to modify the script that a lady (Diane?) kindly attached with reference as to how to open ALL links automatically. I tried this by adding words in the other hyperlinks to hopefully skip over them, to no avail (I'm a complete novice), that she used with unsubscribe. All the links keep on being activated. Is there any way I could just have the third hyperlink activated in my email - it has the word "accept" in the linked URL and the other URL has the word "reject".

I will show you what I have at the moment to try and exclude the other links:


Option Explicit
Public Sub OpenAllMessageLinks(Item As Outlook.MailItem)
Dim objOL As Outlook.Application
Dim objItems As Outlook.Items
Dim objFolder As Outlook.MAPIFolder
Dim olMail As Outlook.MailItem
Dim Reg1 As RegExp
Dim M1 As MatchCollection
Dim M As Match
Dim strURL As String
Dim oApp As Object

Set objOL = Outlook.Application
Set objFolder = objOL.ActiveExplorer.CurrentFolder
Set objItems = objFolder.Items


Dim browserPath As String
browserPath = Chr(34) & "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" & Chr(34)

Set Reg1 = New RegExp

On Error Resume Next

For Each olMail In objItems

With olMail
With Reg1
.Pattern = "(https?[:]//([0-9a-z=\?:/\.&-^!#$;_])*)"
.Global = True
.IgnoreCase = True
End With

If Reg1.Test(olMail.Body) Then

Set M1 = Reg1.Execute(olMail.Body)
For Each M In M1
strURL = M.SubMatches(0)
Debug.Print strURL
If InStr(strURL, "unsubscribe") Then GoTo NextURL
If InStr(strURL, "reject") Then GoTo NextURL
If InStr(strURL, "newsletter") Then GoTo NextURL
If InStr(strURL, "twitter.com") Then GoTo NextURL
If InStr(strURL, "linkedin.com") Then GoTo NextURL

If Right(strURL, 1) = ">" Then strURL = Left(strURL, Len(strURL) - 1)

Shell (browserPath & " -url " & strURL)
DoEvents

NextURL:
Next
End If

End With
Next
Set Reg1 = Nothing
Set objItems = Nothing
Set objFolder = Nothing
Set objOL = Nothing
End Sub

The ones in pink are the added "code" that I'm playing about with, to try and exclude all the other hyperlinks - it doesn't work - they all keep opening.

Any help gratefully received

Amanda
 
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
#2
Amanda, I am trying to accomplish something the same as you. Did you ever find a solution?
Thanks,
Keogh
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#3
This should work... it skips the url if a match is found, if no match, it moves to the next if.
Code:
If InStr(strURL, "unsubscribe") Then GoTo NextURL
If InStr(strURL, "reject") Then GoTo NextURL
If InStr(strURL, "newsletter") Then GoTo NextURL
If InStr(strURL, "twitter.com") Then GoTo NextURL
If InStr(strURL, "linkedin.com") Then GoTo NextURL
If Right(strURL, 1) = ">" Then strURL = Left(strURL, Len(strURL) - 1)
The big thing to verify is if the url contains the words… Debug.Print strURL prints the url to the immediate window but to might be easier during testing to use msgbox strurl so you can see on screen what the url is before it goes though the if's - strURL is just the url, not the display name.


for example, the first one would be picked up by the if statement and skipped but the facebook url would get past the if statement.
HTML:
<a href="https://twitter.com">twitter.com</a>
 <a href="https://list.slipstick.com/l/facebook">facebook.com</a>
 
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
#4
Oh, I see. So even if the hyperlink is attached to the anchored text, it "prints" it out and verifies that the text is actually in the URL? That correct? I will try it out.
How would I get it to kick off ONLY from a specific sender?
Cheers!
Keogh
 
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
#5
Unfortunately, for me, there is no key word in the actual URL that can be used. It is a random string. I need to be able to key on the text the the hyperlink is anchored to.
Diane, I replied in another thread you were on regarding this also. Any ideas?
Automatically open link in email received
Much appreciation.
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#6
correct - the macro only check for the word or phrase in the url, not the anchored text.

Unfortunately, for me, there is no key word in the actual URL that can be used. It is a random string. I need to be able to key on the text the the hyperlink is anchored to.
Hmmm. if the pattern looks for <a... </a> it picks up...
HTML:
<a href="https://list.slipstick.com/l/facebook">facebook.com</a>
and if no matches, a submatch holds the actual url

I didn't test this, so the pattern might not be right but the basic premise should be good - i think this is mostly correct to get the url if there are css directives in the a tag.

Code:
With olMail
With Reg1
.Pattern = "(<a\s*(.*)(https?[:]//([0-9a-z=\?:/\.&-^!#$;_])*\s*(.*))</a>)"
.Global = True
.IgnoreCase = True
End With

If Reg1.Test(olMail.Body) Then

Set M1 = Reg1.Execute(olMail.Body)
For Each M In M1
strURL = M.SubMatches(0)
openURL =M.SubMatches(2)
Debug.Print strURL
Debug.Print openURL

If InStr(strURL, "unsubscribe") Then GoTo NextURL
If InStr(strURL, "reject") Then GoTo NextURL
If InStr(strURL, "newsletter") Then GoTo NextURL
If InStr(strURL, "twitter.com") Then GoTo NextURL
If InStr(strURL, "linkedin.com") Then GoTo NextURL
If Right(strURL, 1) = ">" Then strURL = Left(strURL, Len(strURL) - 1)

Shell (browserPath & " -url " & openURL )
 
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
#7
correct - the macro only check for the word or phrase in the url, not the anchored text.


Hmmm. if the pattern looks for <a... </a> it picks up...
HTML:
<a href="https://list.slipstick.com/l/facebook">facebook.com</a>
and if no matches, a submatch holds the actual url

I didn't test this, so the pattern might not be right but the basic premise should be good - i think this is mostly correct to get the url if there are css directives in the a tag.

Code:
With olMail
With Reg1
.Pattern = "(<a\s*(.*)(https?[:]//([0-9a-z=\?:/\.&-^!#$;_])*\s*(.*))</a>)"
.Global = True
.IgnoreCase = True
End With

If Reg1.Test(olMail.Body) Then

Set M1 = Reg1.Execute(olMail.Body)
For Each M In M1
strURL = M.SubMatches(0)
openURL =M.SubMatches(2)
Debug.Print strURL
Debug.Print openURL

If InStr(strURL, "unsubscribe") Then GoTo NextURL
If InStr(strURL, "reject") Then GoTo NextURL
If InStr(strURL, "newsletter") Then GoTo NextURL
If InStr(strURL, "twitter.com") Then GoTo NextURL
If InStr(strURL, "linkedin.com") Then GoTo NextURL
If Right(strURL, 1) = ">" Then strURL = Left(strURL, Len(strURL) - 1)

Shell (browserPath & " -url " & openURL )
Thank you so much. I think I can get it to work. I think I found something unique in the URL I can match to.
Last thing, how can I get it to process new emails in the inbox from a specific email address? I apologize if my terminology is incorrect.
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#8
if you were using a run a script version, you'd set up the rule to look for that address... in an itemadd, you need to use an if statement.

if item.senderemailaddress = "someone@domain.com" then
'the code goes here
end if

As an FYI, the macro above as written looks like a run a script, but it is not correct - you won't use this in a run a script (or item add macro) For Each olMail In objItems
That line will cause the macro to check every message in the folder, not just the new one.

it should work to replace these two lines and remove the very last Next

For Each olMail In objItems
With olMail


with

with item
 
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
#9
Below is what I have. It builds off of your first example on Open All Hyperlinks in an Outlook Email Message.
What changes would I make to this to trigger on ONLY new mail arriving in the inbox? I have an outlook rule to only call the script when sent by a specific email address.

Sub OpenLinksMessage(Item As Outlook.MailItem)
Dim olMail As Outlook.MailItem
Dim Reg1 As RegExp
Dim M1 As MatchCollection
Dim M As Match
Dim strURL As String
Dim oApp As Object
Set oApp = CreateObject("InternetExplorer.Application")

Set olMail = Application.ActiveExplorer().Selection(1)

Set Reg1 = New RegExp

With Reg1
.Pattern = "(https?[:]//([0-9a-z=\?:/\.&-^!#$;_])*)"
.Global = True
.IgnoreCase = True
End With

If Reg1.Test(olMail.Body) Then

Set M1 = Reg1.Execute(olMail.Body)
For Each M In M1
strURL = M.SubMatches(0)
Debug.Print strURL
If InStr(strURL, "@") Then GoTo NextURL
If InStr(strURL, "test") Then GoTo NextURL
If InStr(strURL, "test2") Then GoTo NextURL
If Right(strURL, 1) = ">" Then strURL = Left(strURL, Len(strURL) - 1)
oApp.navigate strURL, CLng(2048)
oApp.Visible = True

'wait for page to load before passing the web URL
Do While oApp.Busy
DoEvents
Loop

NextURL:
Next
End If

Set Reg1 = Nothing
End Sub
 
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
#11
Remove this - it tells the macro to use the selected item.
Set olMail = Application.ActiveExplorer().Selection(1)

Then change all instances of olMail to Item (or change Item in (Item As Outlook.MailItem) to olMail)

The macro sample at this bookmark Open All Hyperlinks in an Outlook Email Message will work with a rule.
Diane, thank you so much for your assistance. You are a gem. I think I actually got it to function, however Safe Links is somehow disallowing the launch of the allowed URL even though you can manually click on the URL and it launches fine. Anywhere online I can buy you a beer?
Cheers!
Keogh
 
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
#12
I think I may have figured out where the issue is. I hope. Safe Links prepends every URL with "Office365". Everything after the percent sign (including the percent sign) is chopped off of the URL when it is opened in Internet Explorer. I attempted to edit the Reg1 .Pattern to look like below, adding the %, however it is still not seeing the "%". I tried with brackets ([%]) around it also to no avail. Any ideas?

.Pattern = "(https?[:]//%([0-9a-z=\?:/\.&-^!#$;_])*)"
 
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
#13
I think I may have figured out where the issue is. I hope. Safe Links prepends every URL with "Office365". Everything after the percent sign (including the percent sign) is chopped off of the URL when it is opened in Internet Explorer. I attempted to edit the Reg1 .Pattern to look like below, adding the %, however it is still not seeing the "%". I tried with brackets ([%]) around it also to no avail. Any ideas?

.Pattern = "(https?[:]//%([0-9a-z=\?:/\.&-^!#$;_])*)"
Fixed it. Added the "%" towards the end.

.Pattern = "(https?[:]//([0-9a-z=\?:/\.&-^!#$%;_])*)"
 

Similar threads