Use VBA to replace variable in Outlook

jeffshead

New Member
Joined
Nov 25, 2017
Messages
1
I'll do my best to explain what I would like to accomplish but I have zero experience with VBA and I haven't been able to piece together anything that works.

I use Outlook 2010 and I have several different signatures. I have HTML and plain text versions of each signature so the VBA code needs to work with both types of signatures. Each signature contains an unsubscribe link. I need VBA code that can add a parameter to the unsubscribe link. The parameter being the email address of the recipient which is pulled from the TO, CC and BCC fields.

Example of the link in an HTML signature (Had to substitute curly brackets for opening and closing "a" tags):
Code:
{a <a. href="https://www.mysite.com/opt-out/?email=user@domian.tld">href="https://www.mysite.com/opt-out/?email=[B][COLOR=#008000]user@domian.tld[/COLOR][/B]"}Unsubscribe.{/a}</a.>
Example of the text in a plain text signature:
Code:
Use this link to Unsubscribe: https://www.mysite.com/opt-out/?email=[B][COLOR=#008000]user@domian.tld[/COLOR][/B]

I was thinking the best way would be to use a variable and have the VBA code replace the variable with the email address of each recipient.

Example:
Code:
{a href="https://www.mysite.com/opt-out/?email=[B][COLOR=#008000]%RECIPIENT%[/COLOR][/B]"}Unsubscribe.{/a}

I want to use this code so the user can click on the link and be taken to an opt-out page and have the email form filed prefilled for them.

Keep in mind that there may be more than one recipient in the TO, CC or BCC fields. I need one email per recipient with only their email address in the link.

The closest I've come is with the snippet below but it only works with HTML signatures and it uses the recipient's names instead of email addresses. It also adds all recipients to every email which is not what I want:
Code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim m As MailItem
Set m = Application.ActiveInspector.CurrentItem
m.HTMLBody = Replace(m.HTMLBody, "%7b%7bRECIPIENT%7d%7d", m.To & " " & m.CC & " " & m.BCC)
End Sub

I was also thinking that it would be better to hash the email address with salt instead of it being past in plain text, in the URL. For example:
Code:
{a <a. href="https://www.mysite.com/opt-out/?email=user@domian.tld">href="https://www.mysite.com/opt-out/?email=[B][COLOR=#008000]sdfdfh53sdfg55rgfg53sg[/COLOR][/B]"}Unsubscribe.{/a}</a.>
I can then use $_GET on the landing page and PHP to decrypt the hash and prefill the form on the unsubscribe page.

Can this be done with VBA? I haven't found any software or Outlook add-ins that can do this :(

Cheers,

Jeff
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,224,933
Messages
6,181,841
Members
453,068
Latest member
DCD1872

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top