Automatically Insert Recipient Name from To Field

Status
Not open for further replies.

learning321A

Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
Hello, this issue is driving me crazy. I have created a template and a macro that calls that template up from my quick access. It does everything I want EXCEPT I cannot get it to autopopulate a name in the greeting.

So if I type in Joe@blahblah.net, I want the template to insert Joe in the greeting, Dear Joe.

Can someone please tell me how to make this happen? I tried to edit the template but cannot figure out where to add the recipient name tags. Please Help! This should be easy.

I know how to make it happen for a reply. That is not my issue. This is for new emails. I would be happy to write a macro that does it as well. Just need the code for getting it to pull the name. We do not want to use a mail merge here.

Thanks!!
 
you'll need to use a macro - none of the tricks i know of (like using merge fields) will grab the alias from the to field. if you are clicking on a contact to address it, merge fields (and a macro) would grab the contact name.

You'd use something like
strname = Mid(item.to, 1, InStr(1, item.to, "@"))
then
item.body = "Hi " & strName & vbcrlf
 
Hi, thanks, I was hoping it would be you that responds! So I tried that. You mean it would have to be a second macro, or a loop, right? I mean, it will not be able to do so until I type in the email address, so would still have to click the macro, correct? Was hoping to get it to call automatically right after I typed the name.
I would like to also pull the name of the CC. Is there a way to do that? I really thought I could just enter code tags in a template somewhere, but I do not see where I could do that? I would also need to know exactly what the code tags are.

I did try what you posted, should the word 'then' be included? I got an error saying that it needs an object.

Thanks!
 
Was hoping to get it to call automatically right after I typed the name.
i don't think that is covered by an 'after' event - if if it, you could automate it.
 
Okay, so i am going to try another route. Set it up as a reply. Is there a way to get the reply to pull the name of the sender and the CC names and insert them in the reply? If so, how do i do that?
 
You can get the recipient names - this article has the basics for handling the reply and insertion Copy attachment names when replying

You'd use something like this in place of the attachment name code to grab the names - i didn't test it though, so i could have a typo or error in it.
Code:
Dim Recipients As Outlook.Recipients
Dim R As Outlook.Recipient
Dim i, strName

Set Recipients = Item.Recipients
For i = Recipients.Count To 1 Step -1

Set R = Recipients.Item(i)
strname = R.name & "," & strname
Next

splitting the names between who was cc'd and to'd would use an if -

if R.type = olcc then
strCC = R.name & "," & strcc
else
strname = R.name & "," & strname
end if
 
Last edited:
Thanks, sorry, please understand I am new to all this, and do not quite get this. Much of this is far different from how I would code in excel! Where would the if.recipients piece go? Would this all be in a module? I tried to copy/paste this code but it says I need an object for the Set Recipients = Item.Recipients line. I am not sure what that means, or what I have to do next?

Thank you so much again!
 
Using the code sample for the Simple method that reuses a macro for Replies, All, and Forward, this works for the afterreply macro.

Code:
Private Sub afterReply()
    oResponse.Display
' get the recipient names
Dim Recipients As Outlook.Recipients
Dim R As Outlook.Recipient
Dim i
Dim strTo As String, strCC As String

Set Recipients = oResponse.Recipients
For i = 1 To Recipients.Count
Set R = Recipients.Item(i)

Debug.Print R.Name, R.Type

If R.Type = olCC Then
strCC = R.Name & ", " & strCC
Else
strTo = R.Name & ", " & strTo
End If
Next

'insert the names
    Dim olInspector As Outlook.Inspector
    Dim olDocument As Word.Document
    Dim olSelection As Word.Selection

    Set olInspector = Application.ActiveInspector()
    Set olDocument = olInspector.WordEditor
    Set olSelection = olDocument.Application.Selection

  olSelection.InsertBefore "CC: " & strCC
  olSelection.InsertParagraphBefore
  olSelection.InsertBefore "To: " & strTo

End Sub
 
Thanks, sorry, please understand I am new to all this, and do not quite get this. Much of this is far different from how I would code in excel! Where would the if.recipients piece go? Would this all be in a module? I tried to copy/paste this code but it says I need an object for the Set Recipients = Item.Recipients line. I am not sure what that means, or what I have to do next?

Thank you so much again!
it needs to go in ThisOutlookSession, as its a startup macro.

I need an object for the Set Recipients = Item.Recipients
it was telling you that Item. wasn't assigned to an object (in this case, the object is the message you are replying to or the actual reply)
 
Okay, so I put it into the ThisOutlookSession as you pointed out. I saved and closed Outlook, then restarted it. Should it do something now automatically? I tried to click reply but nothing was different.
 
When I try to run the macro directly, it gives a compile error: User-defined type not defined.
It highlights this line in blue: olDocument As Word.Document
Is that a problem?
 
You need to set a reference to the the word object model.
 
BTW, rather than restart outlook all the time, you can click in Application_Startup and press Run.
 
Hi, I am sorry, I am not sure what this means:
You need to set a reference to the the word object model.

If I do that, should my outlook now start pulling in the names with replies?
 
Okay, I read what you wrote and I think I added that word library. But still nothing happens. When I go into the VBA editor and try to run it directly, i get this error: run time 424: Object required.
 
You can't run the afterReply macro manually - it doesn't set the selected message as the one to reply to (causing the object required message). You can kick start the auto start macro then click Reply All to test it.

Or you can use this macro to run afterReply on the selected message:

Code:
Public Sub TestMacro()
    Dim objOL As Outlook.Application
    Dim oItem As MailItem
    Set objOL = Outlook.Application
    Set oItem = objOL.ActiveExplorer.Selection.Item(1)
    Set oResponse = oItem.ReplyAll
  
    afterReply

   Set objOL = Nothing
End Sub
 
Ok thanks, so sorry again, you clearly know this stuff! I, however, do not but am trying to keep up with you! When you say kick start the auto start macro, what do you mean? Should this just work when I hit reply all, inserting the names?

Then, when you say i can run that other code, is that in place of the whole large code I already put in, or with it? do I put that in the same place as I put the afterReply?

Thanks!
 
Should this just work when I hit reply all, inserting the names?
Yes. it should just work when you hit reply.

Application_start macros run when you start outlook (just like similar macros do in word and excel)... but it's a PITA to restart outlook when you are testing macros. So, instead of restarting outlook, you can click in application_start and click run. It makes outlook think you restarted it.

that other code, is that in place of the whole large code I already put in
No, its not replacing the other code. it's just a little stub to trigger the afterReply code. (Assuming you are using the two-part code in VBA Sample: Do Something When Reply is Clicked - I added a version of the afterReply macro to add the names to this page).

These little stub macros are really helpful when you are testing run a script rules (avoiding the need to send new mail). They are less useful with this macro because you just need to click Reply, but it saves a step or two since you won't need to go back into Outlook's main window to hit reply - as long as a message is selected, you can test the macro from the vba editor by running the TestMacro stub. It uses the selected message and triggers a replyall, then calls the afterReply macro. If it works perfectly but hitting reply doesn't, then the problem is that the application_start macro wasn't started (or was stopped when a macro errored).

paste it at the end of the thisoutlooksession module.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
C Automatically Insert Recipient Name from To Field Outlook VBA and Custom Forms 4
X Run macro automatically when a mail appears in the sent folder Using Outlook 5
J Calendar events created on iPhone automatically changing default alert from 'None' to 'Time of Event' Using Outlook.com accounts in Outlook 0
Z Automatically adjust Outlook Reading Pane from bottom to right depending on portrait or landscape window Using Outlook 1
G Automatically delete email when a condition is met Outlook VBA and Custom Forms 1
Hornblower409 Automatically or Manually Backup Multiple Versions of VbaProject.OTM Outlook VBA and Custom Forms 1
B Outlook 2019 Automatically move email after assigning category Using Outlook 4
G automatically choosing "add to autocorrect" option Using Outlook 0
L Why are some email automatically going to "archive" Using Outlook 2
Z Outlook 365 Automatically assign categories to incoming mail in a shared folder Round Robin Outlook VBA and Custom Forms 1
G Automatically delete messages in the synchronization folder Outlook VBA and Custom Forms 3
E Remove flag automatically Using Outlook 4
T Outlook 365 Move newly created tasks automatically on save. Outlook VBA and Custom Forms 1
M Outlook 365 Switching from AOL to Yahoo automatically Using Outlook 5
P Print attachments automatically and move the mail to an existing folder called "Ted" Outlook VBA and Custom Forms 4
B Zoom automatically next email item (VBA) Outlook VBA and Custom Forms 2
Paul Hobbs Automatically accept "Empty Folders" prompt Outlook VBA and Custom Forms 6
diver864 vba for a rule to automatically accept meeting requests with 'vacation' in subject, change to all-day event, change to free, don't send reply Outlook VBA and Custom Forms 1
D Custom Search Folders not refreshing/updating automatically Using Outlook 0
M Automatically add senders first name to a greeting Outlook VBA and Custom Forms 1
C Add Form to Appointments Received, Automatically Outlook VBA and Custom Forms 6
J Automatically forward email and apply template Outlook VBA and Custom Forms 0
Y Outlook 2013 Stop Outlook from automatically assigning categories to Tasks Using Outlook 0
O Forward a email with modified body Automatically. Outlook VBA and Custom Forms 0
A How to open a specific link automatically with outlook 2016 Outlook VBA and Custom Forms 6
P Automatically Categorize Meetings once they are accepted Outlook VBA and Custom Forms 5
W Automatically open attachments without automatically printing them Using Outlook 0
N How to set automatically the default or user defined Quickstyle Templates by Answer in Outlook Using Outlook 1
O Run macro automatically at sending an email Using Outlook 11
D Outlook 2016 automatically increment anniversaries Using Outlook 1
T Office 2013 no longer updating automatically Using Outlook 2
B Automatically Forward Emails and Remove/Replace All or Part of Body Outlook VBA and Custom Forms 8
D Print attachments automatically and moves the mail to a new folder Outlook VBA and Custom Forms 9
A How to open a specific link automatically with outlook Outlook VBA and Custom Forms 13
N how to sync automatically when outlook opens Using Outlook 10
A Sort emails into subfolders based on sender and deleting emails automatically Outlook VBA and Custom Forms 3
undercover_smother Automatically Forward All Sent Mail and Delete After Send Outlook VBA and Custom Forms 10
C Need VBA code to automatically save message outside outlook and add date Outlook VBA and Custom Forms 1
stephen li VBA Outlook send mail automatically by specified outlook mail box Outlook VBA and Custom Forms 1
R Make Enter Network Password Prompt Go Away Automatically Using Outlook 0
I Print Automatically Attachments Outlook VBA and Custom Forms 3
S Automatically selecting folders and deleting messages in Outlook VBA Outlook VBA and Custom Forms 7
M Outlook 2016 Rules Not Working Automatically Using Outlook 5
Diane Poremsky Automatically create a task when sending a message Using Outlook 0
D Is it possible to automatically send an email when it is moved to a folder? Exchange Server Administration 1
A Automatically send email based on drop-down field? Outlook VBA and Custom Forms 2
M Automatically create event in calendar when task is created Outlook VBA and Custom Forms 1
Diane Poremsky Create Appointment From Email Automatically Using Outlook 0
Cameron Piper Automatically update custom forms across multiple computers Outlook VBA and Custom Forms 1
T Automatically open link in email received Outlook VBA and Custom Forms 33

Similar threads

Back
Top