Solved

Automatically appending a suffix to Email recipient's address

Posted on 2014-02-25
20
609 Views
Last Modified: 2014-02-26
Running Outlook 2007, I use an Email tracking service which requires manually inserting a suffix to every recipient's address.  For example, instead of addressing a message to Tom@gmail.com  I would need to type Tom@gmail.com.WhoReadMe.com    There are other tracking services which provide a plug-in to automate this step, but the service I prefer to use doesn't have that feature.  Can you supply code for a macro which will add a suffix to every recipient's Email address?
0
Comment
Question by:ddantes
  • 10
  • 10
20 Comments
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
I'm assuming you want that suffix added to every address for every message.  Is that correct?  If so, would you like that to happen as you send messages?
0
 

Author Comment

by:ddantes
Comment Utility
Thank you for your participation.  Yes, the suffix would be added to every address for every message, as the message is being sent.  However, I'd like to activate this process with a rule, so that it could be turned off in some instances.
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
It is not possible to do this with a rule.  It requires a script.  I can write the script so you can enable/disable it with a single click.  Will that work?
0
 

Author Comment

by:ddantes
Comment Utility
A script which always adds the suffix by default, but can be disabled or re-enabled with a single click would seem workable.
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Here's the code.  Follow these instructions to add it to Outlook.

1.  Copy the code below and paste into the ThisOutlookSession module in Outlook.
2.  Choose one of the methods from this page for adding a button that runs a macro.  Set the button to run the macro named ToggleAddSuffix.  Clicking the button will flip the option to add the suffix on and off.  When it's on, the code in Application_ItemSend will add the suffix to all addresses of any email you send.  

Dim bolAddSuffix As Boolean

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim olkRcp As Outlook.RECIPIENT, olkNew As Outlook.RECIPIENT, intCnt As Integer
    If bolAddSuffix Then
        If Item.Class = olMail Then
            For intCnt = Item.Recipients.Count To 1 Step -1
                Set olkRcp = Item.Recipients.Item(intCnt)
                Set olkNew = Session.CreateRecipient(olkRcp.Address & ".WhoReadMe.com")
                olkNew.Resolve
                olkRcp.Delete
                Item.Recipients.Add olkNew
            Next
            Item.Save
        End If
    End If
End Sub

Sub ToggleAddSuffix()
    bolAddSuffix = Not boAddSuffix
    MsgBox "AddSuffix is now " & IIF(bolAddSuffix, "ON", "OFF"), vbInformation+vbOkOnly, "Toggle Add Suffix"
End Sub

Open in new window

0
 

Author Comment

by:ddantes
Comment Utility
Thank you!
Testing this code, I'm having a couple of challenges...

1. The toggle button on the tool bar doesn't turn suffix off.  Every click of the button produces the message "AddSuffix is now ON.";

2. Right-clicking the toolbar button, there is no option to rename it.

3. The suffix is added to outgoing messages, but the messages are not delivered:

"Your message did not reach some or all of the intended recipients.
      Subject:      WRM suffix5
      Sent:      2/25/2014 11:56 AM
The following recipient(s) cannot be reached:"

If I add the suffix manually, the message is delivered.
I've attached a screen shot of ThisOutlookSession.
Image2.jpg
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
1. The toggle button on the tool bar doesn't turn suffix off.  Every click of the button produces the message "AddSuffix is now ON.";

 There's a typo on line #20 of the code.  Line 20 should read

bolAddSuffix = Not bolAddSuffix

Open in new window


2. Right-clicking the toolbar button, there is no option to rename it.

I don't know which method you chose for adding the button, so I'm not clear on the problem is or how to fix it.

3. The suffix is added to outgoing messages, but the messages are not delivered:

Did I add the suffix correctly?  Take a look at a sent message to see if I got it right.  If I did get it right and the messages aren't being delivered, then I don't have an answer.  If I got it wrong, then tell me how it's wrong and I'll fix it.
0
 

Author Comment

by:ddantes
Comment Utility
Thank you.  Correcting the typo allows the function to be toggled.
The correct suffix is added.  If I toggle the automatic suffix OFF, and insert an identical suffix manually, it is delivered without that Administrative message.

I believe there could be an issue with how the recipient's address is inserted into the outgoing message.  If I compare a failed message with automatic suffix to a successful message with manual suffix, there is a difference:  double-clicking the recipient's address in the successful message opens up a dialog box "Email Properties".  Double-clicking the recipient's address in the failed message does not open anything.  I'm attaching both messages, hoping your examination will be helpful.manualsuffix.msgautosuffix.msg

The method used for adding a macro button to the tool bar was Customize>Macros, then drag the macro onto the tool bar.
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
I looked at the properties of both messages.  The recipient on the autosuffix message doesn't have an email address while the recipient in the manualsuffix message does.  That's odd.  I tested the code again and on my computer the address is filled in properly.  I'm at a bit of a loss to understand why it isn't on your computer.  The best I can figure is that on your computer Outlook hasn't resolved the address by the time the code runs.  It should have, and it does on my computer, but the evidence suggest that it hasn't on your computer.  Assuming that's the problem, then adding this line of code

olkRcp.Resolve

Open in new window


immediately after line #8 of the code in my earlier post should fix that problem.  Please give that a try ad let me know if that does it.
0
 

Author Comment

by:ddantes
Comment Utility
Thank you.  I inserted the new line of a code as a new line, after line #8.  Unfortunately, the behavior is the same.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Then I'm stumped. I don't use the service, so I can't do a live test. The closest I can come is to go through the motions and stop the message before it leaves my outbox. Everything looks right at that point. I don't know what's happening after that.
0
 

Author Comment

by:ddantes
Comment Utility
I don't think this is related to the service.  The message from Administrator appears less than one second after sending the message.  It says the message could not be delivered to the following recipients, and that field is blank.  I tried replacing ".whoreadme.com" with "", so the message was sent without a suffix.  Same undeliverable error.

On a positive note, I found out how to rename the macro button on the toolbar.
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
It sounds like the address is disappearing somehow.  I don't know why that would happen or what could cause it.  Do you have any Outlook add-ins installed or any other code that affects outbound messages?
0
 

Author Comment

by:ddantes
Comment Utility
I have some Outlook add-ins.  However, adding the suffix manually doesn't interfere with delivery.  I disabled all the add-ins except for Microsoft VBA for Outlook, which is required to run your script, and it didn't make a difference.

There are other scripts running in Outlook, including one which you provided, in response to another EE question.   I tried removing everything in ThisOutlookSession except for AddSuffix, and everything in two additional Modules, and it didn't make any difference.
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
However, adding the suffix manually doesn't interfere with delivery.

Yes, but you're entering that manually.  Something is happening when the code tries to modify the addresses.  I was hedging against the possibility that some other add-in or macro was interfering with the add suffix code.

The only thing left to do is to try some debugging to try and figure out what's happening and why.  

1.  Replace the code you have now with the version below.  
2.  Turn AddSuffix on
3.  Create a new email
4.  Address it to one address
5.  Send the message
6.  Switch back to the VB editor
7.  If the "Immediate" pane is not opened, then open it by pressing CTRL+G
8.  The "Immediate" pane should contain some output that looks like this

AddSuffix is turned on
The item is an email.
Recipient #1
Old Address: bugs@looneytunes.com
New Address: bugs@looneytunes.com.WhoReadMe.com


9.  Paste the results in your response.

Dim bolAddSuffix As Boolean

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim olkRcp As Outlook.RECIPIENT, olkNew As Outlook.RECIPIENT, intCnt As Integer
    If bolAddSuffix Then
        Debug.Print "AddSuffix is turned on"
        If Item.Class = olMail Then
            Debug.Print "The item is an email."
            For intCnt = Item.Recipients.Count To 1 Step -1
                Debug.Print "Recipient #" & intCnt
                Set olkRcp = Item.Recipients.Item(intCnt)
                olkRcp.Resolve
                Debug.Print "Old Address: " & olkRcp.Address
                Set olkNew = Session.CreateRecipient(olkRcp.Address & ".WhoReadMe.com")
                olkNew.Resolve
                Debug.Print "New Address: " & olkNew.Address
                olkRcp.Delete
                Item.Recipients.Add olkNew
            Next
            Item.Save
        End If
    End If
End Sub

Sub ToggleAddSuffix()
    bolAddSuffix = Not bolAddSuffix
    MsgBox "AddSuffix is now " & IIf(bolAddSuffix, "ON", "OFF"), vbInformation + vbOKOnly, "Toggle Add Suffix"
End Sub

Open in new window

0
 

Author Comment

by:ddantes
Comment Utility
Thank you for the debug script.  The "undeliverable" notice appeared.  Here are the contents of the "immediate" pane:

The item is an email.
Recipient #1
Old Address: ddantes@compuserve.com
New Address: ddantes@compuserve.com.WhoReadMe.com

I have four machines in my office -- two desktops running Outlook 2007 on Windows 7 and two notebooks running Outlook 2007 on Windows XP.  I've tried the script on all of them, but no joy.  Both Windows 7 machines have the "undelivered" message.  Both Windows XP machines go into a state where the send/receive progress window appears to be vibrating, and the number of "completed tasks" continuously increases, into the triple digits, until I close Outlook.
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
Comment Utility
Ok, I set up my own WhoReadMe account and did some testing.  For some reason the address isn't being resolved even though I issued the Resolve command on line #15.  I was finally able to get it to work by resolving a second time just before the mail goes out.  Insert this line

Item.Recipients.ResolveAll

Open in new window


after line #19 of the version you have right now and try again.
0
 

Author Comment

by:ddantes
Comment Utility
That worked totally!   Thank you for your perseverance.
0
 

Author Comment

by:ddantes
Comment Utility
I have a request for a possible refinement to this code.  If you're available to look at it, please see

http://www.experts-exchange.com/Software/Office_Productivity/Groupware/Outlook/Q_28375379.html
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
You're welcome.  Sorry it took me so long to figure it out.

I'll go have a look at your new question.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now