Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 18-Jun-11 22:50pm
v2

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.
 
Share this answer
 
Comments
Member 7746250 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?
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 :)
 
Share this answer
 
Comments
Member 7746250 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900