Automatically Insert Recipient Name from To Field

Status
Not open for further replies.

learning321A

Member
Best answers
0
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!!
 

Diane Poremsky

Senior Member
Best answers
0
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

learning321A

Member
Best answers
0
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
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!
 

learning321A

Member
Best answers
0
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
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?
 

Diane Poremsky

Senior Member
Best answers
0
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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:

learning321A

Member
Best answers
0
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
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!
 

Diane Poremsky

Senior Member
Best answers
0
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

Diane Poremsky

Senior Member
Best answers
0
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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)
 

learning321A

Member
Best answers
0
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
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.
 

learning321A

Member
Best answers
0
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
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?
 

Diane Poremsky

Senior Member
Best answers
0
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
You need to set a reference to the the word object model.
 

Diane Poremsky

Senior Member
Best answers
0
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
BTW, rather than restart outlook all the time, you can click in Application_Startup and press Run.
 

learning321A

Member
Best answers
0
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
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?
 

learning321A

Member
Best answers
0
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
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.
 

Diane Poremsky

Senior Member
Best answers
0
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

learning321A

Member
Best answers
0
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
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!
 

Diane Poremsky

Senior Member
Best answers
0
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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.
Top