Outlook VBA script - find and replace text with image
Posted
by
user2530616
on Super User
See other posts from Super User
or by user2530616
Published on 2013-06-28T06:51:38Z
Indexed on
2013/06/28
10:23 UTC
Read the original article
Hit count: 305
microsoft-outlook
|vba
I have a e-commerce store. When I get a sale, I receive an order confirmation email which contains the name of the product sold. When the email comes through, I would like to run a script that replaces the product name eg. "red widget", with a picture of that product. Is that possible?
I have found a similar code to replace text (set of numbers in this case) with a link, but I need it to replace with a picture instead.
Option Explicit
Sub InsertHyperLink(MyMail As MailItem)
Dim body As String, re As Object, match As Variant
body = MyMail.body
Set re = CreateObject("vbscript.regexp")
re.Pattern = "#[0-9][0-9][0-9][0-9][0-9][0-9]"
For Each match In re.Execute(body)
body = Replace(body, match.Value, "http://example.com/bug.html?id=" & Right(match.Value, 6), 1, -1, vbTextCompare)
Next
MyMail.body = body
MyMail.Save
End Sub
example mail
Order Confirmation
Thanks for shopping with us today!
------------------------------------------------------
Order Number: 2209 Date Ordered: Friday 28 June, 2013Products
------------------------------------------------------
1 x red widget = $5.00
------------------------------------------------------
Total: $0.00Delivery Address xxx
search text: "red widget"
replace picture: redwidget.jpg
© Super User or respective owner