# outlook-Automatically Insert Recipient Name from To Field



## Trevor3007 (May 27, 2021)

hi,

i know this is possible  as i have seen it, but I cannot now find it on the web ( some very near to, but not actual)

From the  To: field bill.james@riddle.com

and in the body of message

the VB will automatically put in

Hello bill, (including "," )

This would be ran from a macro, can someone much much cleaver than I,  send me the applicable VB code please


----------



## Domenic (May 30, 2021)

Have you considered creating a template email for this recipient?


----------



## Trevor3007 (Jun 1, 2021)

Domenic said:


> Have you considered creating a template email for this recipient?


hi Domenic,
yes I have, but I have over 1500 to do and cannot multi send or bcc. each have to have a 'individual' email sent to them with in the body of the message

hi 'name'

thanks anyhoo


----------



## Domenic (Jun 1, 2021)

In that case, you'll need to provide additional information, explaining exactly how you want the process to unfold, and then someone will likely be able to help.

Cheers!


----------



## Trevor3007 (Jun 2, 2021)

ok thank you.




from the the example above, copy jonathan

into the body of the message and after

Hi , 

so it will look like

Hi jonathan ,

hoping this helps & thanks again.


----------



## Domenic (Jun 2, 2021)

Unfortunately, you haven't added any new information, so it's unclear as to how you want the process to unfold.  Are you running the macro from Excel or Outlook?  Are you creating a new email or do you want that information copied to an existing and active email?  

Here's a macro that you can run from Excel.  It simply creates a new email, fills in the relevant fields, and displays the email...


```
Sub Insert_Recipient()

    Dim outlookApp As Object
    Set outlookApp = CreateObject("Outlook.Application")
    
    Dim outlookMailItem As Object
    Set outlookMailItem = outlookApp.CreateItem(0)
    
    With outlookMailItem
        .to = "bill.james@riddle.com"
        .body = "Hi Bill," & vbCrLf & vbCrLf
        .display
        '.send
    End With
    
    Set outlookMailItem = Nothing
    Set outlookApp = Nothing
```


----------



## Trevor3007 (Jun 2, 2021)

thanks.. sorry for not .

(may bad) I should of also stated that the VB has to run from outlook as the excel spreadsheet I am using is on a shared platform & using VB on it causes issues. ?


----------



## Domenic (Jun 2, 2021)

In that case, you can change it to the following...


```
Sub Insert_Recipient()

    Dim theMailItem As MailItem
    Set theMailItem = Application.CreateItem(olMailItem)
    
    With theMailItem
        .To = "bill.james@riddle.com"
        .Body = "Hi Bill," & vbCrLf & vbCrLf
        .Display
        '.send
    End With
    
    Set theMailItem = Nothing
    
End Sub
```


----------



## Trevor3007 (Jun 2, 2021)

Many I will give it test Thursday and advise u soon as .


----------



## Trevor3007 (Jun 2, 2021)

hi,
I could not wait until Thursday to test.

Ran the VB , but it changes the  the recipient name & changes it to whats in the VB (bill.james@riddle.com)


and in the body of the message

inserts "Hi Bill,"


----------



## Trevor3007 (May 27, 2021)

hi,

i know this is possible  as i have seen it, but I cannot now find it on the web ( some very near to, but not actual)

From the  To: field bill.james@riddle.com

and in the body of message

the VB will automatically put in

Hello bill, (including "," )

This would be ran from a macro, can someone much much cleaver than I,  send me the applicable VB code please


----------



## Domenic (Jun 2, 2021)

What should happen instead?


----------



## Trevor3007 (Jun 3, 2021)

Hi
Rather than having to add a recipient each time in your code , I just want to put the recipient email address into 'to' and then the VB will then do the rest..
So 
To:Jon.brown@test.com

The vb will put into the message body 

Hi Jon. ,

Thank you and I hope this helps ?


----------



## Domenic (Jun 3, 2021)

Oh I see, it looks like I completely misunderstood what you are trying to do.  So basically you want to manually create an email, enter the email address in the 'To' field, and then have VBA insert the salutation in the body of the email, correct?  If so, assuming that the email address follows the same format (first name, period, lastname and so on), try something like this...


```
Option Explicit

Sub Insert_Salutation()

    If Application.ActiveInspector Is Nothing Then
        MsgBox "No email is active!", vbExclamation
        Exit Sub
    End If
    
    If TypeName(Application.ActiveInspector.CurrentItem) <> "MailItem" Then
        MsgBox "No email is active!", vbExclamation
        Exit Sub
    End If
    
    With Application.ActiveInspector.CurrentItem
    
        Dim theEmailAddress As String
        theEmailAddress = .To
        
        If Len(theEmailAddress) > 0 Then
            .Body = "Hi " & Split(theEmailAddress, ".")(0) & "," & vbCrLf & vbCrLf
            .GetInspector.WordEditor.Application.Selection.endkey unit:=6 'optional (to position cursor after the salution)
        Else
            MsgBox "The 'To' field is missing!", vbExclamation
        End If
        
    End With
    
End Sub
```

Does this help?


----------



## Trevor3007 (Jun 3, 2021)

BRILLANT....wow...thats not only fantastic, its COOL too   


thank you so much . have a great rest of your day.


have a


----------



## Domenic (Jun 3, 2021)

That's great, I'm glad I was able to help.

Cheers!


----------



## Trevor3007 (Jun 3, 2021)

is it possible for this run automatically in the applicable template rather then run the  macro?


----------



## Trevor3007 (Jun 4, 2021)

Hi Domenic,

i ran your code in the live situ today , yes it does its thing, but it wipes out all the other wording contained  in the template after  Hi Jon,




am sorry this is totally my fault for not explaining this in my original post.



This is code use ATM, to open the OST


```
Sub SM()
Set newItem = Application.CreateItemFromTemplate("C:\Users\JJ\data\data2663.oft")
newItem.Display
Set newItem = Nothing
End Sub
```


is it also possible to combine both your code with the above? I presume that there would have to be a pause/message box to allow the recipients email into the To:
then after that, click on continue?

again , my apologies for not stating this previously.. as you can see, I am no coder.

if you can please can sort , that would be great,


----------



## Domenic (Jun 4, 2021)

The following macro will prompt the user to enter an email address.  If the user cancels or omits the email address, it exits the sub.  Otherwise, it proceeds to create an email from the template, enters the emails address, and then prepends the salutation to the body of the email.  Note, though, you'll need to validate the email address provided by the user.


```
Sub SM()

    Dim theEmailAddress As String
    theEmailAddress = InputBox("Please enter an email address...", Title:="Email Address")
    
    If Len(theEmailAddress) = 0 Then Exit Sub 'user cancelled or omitted the email address
    
    'here you will need to validate the email address
    
    Dim newItem As MailItem
    Set newItem = Application.CreateItemFromTemplate("C:\Users\JJ\data\data2663.oft")
    
    With newItem
    
        .Display
        
        .To = theEmailAddress
        
        .Body = "Hi " & Split(theEmailAddress, ".")(0) & "," & vbCrLf & vbCrLf & .Body
        .GetInspector.WordEditor.Application.Selection.endkey unit:=6 'optional (to position cursor after the salution)

    End With
    
    Set newItem = Nothing
    
End Sub
```

Hope this helps!


----------



## Trevor3007 (Jun 6, 2021)

Domenic said:


> The following macro will prompt the user to enter an email address.  If the user cancels or omits the email address, it exits the sub.  Otherwise, it proceeds to create an email from the template, enters the emails address, and then prepends the salutation to the body of the email.  Note, though, you'll need to validate the email address provided by the user.
> 
> 
> ```
> ...



Morning Domenic,

sorry for the late reply.. not long tested your brill code.

Works great, Thats fantastic & sooo cool too:}

Have a great rest of your day


----------



## Domenic (Jun 6, 2021)

That's great, glad I could help.

And thanks for the feedback.

Cheers!


----------



## Trevor3007 (May 27, 2021)

hi,

i know this is possible  as i have seen it, but I cannot now find it on the web ( some very near to, but not actual)

From the  To: field bill.james@riddle.com

and in the body of message

the VB will automatically put in

Hello bill, (including "," )

This would be ran from a macro, can someone much much cleaver than I,  send me the applicable VB code please


----------



## Trevor3007 (Jun 6, 2021)

Praise and thanks are justly deserved


----------



## Domenic (Jun 6, 2021)

Cheers!


----------



## Trevor3007 (Nov 30, 2021)

Domenic said:


> The following macro will prompt the user to enter an email address.  If the user cancels or omits the email address, it exits the sub.  Otherwise, it proceeds to create an email from the template, enters the emails address, and then prepends the salutation to the body of the email.  Note, though, you'll need to validate the email address provided by the user.
> 
> 
> ```
> ...


many thanks. I tweaked the code to my needs and works OK  for me. Thank you


----------



## Kalbright (Apr 27, 2022)

Domenic said:


> That's great, glad I could help.
> 
> And thanks for the feedback.
> 
> Cheers!


Hi Domenic, 

I know this post is nearing a year old but after hours of searching today, this post is the closest I've come to what I'm trying to achieve. Like Trevor, your original code works nearly perfectly with the following exceptions:

- I'd like it to only pull the first name (currently pulling first and last)
- I'd like to retain my email signature (like Trevor mentioned, this code "wipes out all the other wording", but, unlike Trevor, I'm not using a template so your amended solution to pull the template back in doesn't work for me. 
- I'd like to also populate what goes in the subject line

Functionally, how this goes down for me: CTRL+N for a new window to compose an email > window launches with email signature inserted > hit my macro button from the quick access toolbar > your code runs, inserts "Hi John Doe, ", leaves the cursor at the end of the greeting (perfect! and I want this to remain) but removes the email signature. Is there a way to insert this salutation without removing pre-existing text?


----------

