Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB Outlook VBA
I want to search for ASA1234yy in the body of a text and replace it with the hyperlink ASA1234yy
 
Can you help me to get started. I have tried using Replace function, but it Replaces only text. I want the text to be replaced with a hyperlink.
 
Code done so far
 
Sub ConvertToHyperlink(MyMail As MailItem)
Dim strID As String
Dim Body As String
Dim objMail As Outlook.MailItem
Dim temp As String
Dim RegExpReplace As String
Dim RegX As Object
strID = MyMail.EntryID
 
Set objMail = Application.Session.GetItemFromID(strID)
Body = objMail.Body
Body = Body + "Test"
objMail.Body = Body
 
Set RegX = CreateObject("VBScript.RegExp")
With RegX
.Pattern = "ASA[0-9][0-9][0-9][0-9][a-z][a-z]"
.Global = True
.IgnoreCase = Not MatchCase
End With
RegExpReplace = RegX.Replace(Body, "http://www.code.com/" + RegX.Pattern + "/ABCD")
 
Set RegX = Nothing
objMail.Body = RegExpReplace
objMail.Save
Set objMail = Nothing
End Sub
 
I need to have a output like this
 
For example :If there is some ASA1234yy in the body of a text we should replace it with their specific embedded hyperlink in ASA1234yy.The hyperlink should have the address http://www.code.com/ASA1234yy/ABCD
 
Please help me.
Posted 18-Jun-11 11:12am
Edited 18-Jun-11 23:50pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi
 
Considering that you want to replace text like "ASA1234yy" with a link like "http://www.code.com/ASA1234yy/ABCD", please do the following ,
 
 
Set RegX = CreateObject("VBScript.RegExp")
sBody ="gshaai sdijfASA1234yy dds"
msgbox isObject(RegX)
With RegX
.Pattern = "ASA\d{4}\w{2}"
.Global = True
.IgnoreCase = Not MatchCase
End With
RegExpReplace = RegX.Replace(sBody, "http://www.code.com/ASA1234yy/ABCD")
msgbox RegExpReplace 
 
 
Your code was Ok only thing I changed is the regex pattern.
 
Now, in case you want to replace each captured value inside your http url, the you need to get all matches using the pattern first then you have to update the url,helper code is here,
 
Set RegX = CreateObject("VBScript.RegExp")
sBody = "This is ASA4567sa a sampleASA3333df text to show ASA5555sastring replacement in betASA9999ttween"
msgbox isObject(RegX)
With RegX
.Pattern = "ASA\d{4}\w{2}"
.Global = True
.IgnoreCase = Not MatchCase
End With
Set matches = RegX.Execute(sBody)
msgbox matches.Count
If matches.Count > 0 Then    
    For Each match In matches
       RegExpReplace = RegX.Replace(sBody, "http://www.code.com/"+match.value+"/ABCD")      
    Next
Else
    msgbox "No match", 0, "ADT"
End If
msgbox RegExpReplace 
 
Above code:
Input string : "This is ASA4567sa a sampleASA3333df text to show ASA5555sastring replacement in betASA9999ttween"
Output String : "This is http://www.code.com/ASA9999tt/ABCD a samplehttp://www.code.com/ASA9999tt/ABCD text to show http://www.code.com/ASA9999tt/ABCDstring replacement in bethttp://www.code.com/ASA9999tt/ABCDween"
[Replaced 4 matches]
 
Let me know if that helps.
  Permalink  
Comments
Member 7746250 at 18-Jun-11 18:11pm
   
Hi Arindam,
Thanks this worked.
If I want to replace, this with a embedded hyperlink. i.e just displaying ASA4567sa
ASA9999tt, which when clicked on it will take us to a website. Are you able to get me?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
Try
 
 
       RegExpReplace = RegX.Replace(sBody, "<a href='http://www.code.com/'"+match.value+"'/ABCD'>match.value</a>")      
 
But please make sure that your mail message is of type other that simple "text", it need to be html type mail message.
 
Let me know if that helps Smile | :)
  Permalink  
Comments
Member 7746250 at 19-Jun-11 23:04pm
   
Nope...This does not work. also your previous code which you helped me to get through has a bug. The bug is that, it replaces all text with the pattern to hyperlinks, but uses the last pattern to replace them all.
For eg:
 
ASA4567ss
ASA7888hh
ASA5555fd
 
is replaced as
 
www.code.com/ASA5555fd/ABCD
www.code.com/ASA5555fd/ABCD
www.code.com/ASA5555fd/ABCD
 
Can you please help me on solving this.
 
Also, it would be great if I get just the text displayed, instead of the entire hyperlink.
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 410
1 Jochen Arndt 200
2 Richard MacCutchan 135
3 DamithSL 105
4 PIEBALDconsult 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 19 Jun 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100