outlook-Automatically Insert Recipient Name from To Field

Trevor3007

Well-known Member
Joined
Jan 26, 2017
Messages
667
Office Version
  1. 365
Platform
  1. Windows
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
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
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.

VBA Code:
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!
many thanks. I tweaked the code to my needs and works OK for me. Thank you
 
Upvote 0
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?
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,449
Members
448,966
Latest member
DannyC96

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