outlook-Automatically Insert Recipient Name from To Field

Trevor3007

Well-known Member
Joined
Jan 26, 2017
Messages
675
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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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 ?
 
Upvote 0
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...

VBA Code:
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?
 
Upvote 0
BRILLANT....wow...thats not only fantastic, its COOL too :cool:


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


have a
 
Upvote 0
is it possible for this run automatically in the applicable template rather then run the macro?
 
Upvote 0
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

VBA Code:
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,
 
Upvote 0
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!
 
Upvote 0
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!

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
 
Upvote 0
That's great, glad I could help.

And thanks for the feedback.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,223,959
Messages
6,175,645
Members
452,663
Latest member
MEMEH

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