insert name into outlook

Trevor3007

Well-known Member
Joined
Jan 26, 2017
Messages
567
Hi,

Dont even know if this is possible/viable?

I currently use the VB below in outlook( basically, it opens a predefined template)

VBA Code:
Sub MakeItem()
Set newItem = Application.CreateItemFromTemplate("C:\Users\user\Documents\newphones.oft")
newItem.Display
Set newItem = Nothing
End Sub

once ran, I then insert the 'recipient's first name into & in the body of the message after hi (for example Hi John,).

Is it possible that after i run the above code, enter the recipients email address, that thier first name can be automatically add into the body of the message, after "Hi and before the ","

I hope this makes sense & some VB god can sort for me?

Many thanks in advance.
Kind regards
Trevor :cool:
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,502
Office Version
  1. 2013
Platform
  1. Windows
The easiest way to do this is to include a unique placeholder in your template where the name should go, e.g.:

Hi $%name%$,
... bla di bla di bla ...,
Regards,
Trevor3007

Code below might give you a starting point and is running within the VBE of Excel as well. Of course the e-mail address must be suitable for this method, it is therefore just an example.
VBA Code:
Sub Trevor3007()

    Const PLACEHOLDER As String = "$%name%$"
    
    Dim olApp       As Object
    Dim newItem     As Object
    Dim EmailAddr   As String
    Dim FirstName   As String

    EmailAddr = "Joe.Bonamassa@domain.com"
    FirstName = Left(EmailAddr, InStr(EmailAddr, ".") - 1)

    Set olApp = VBA.CreateObject("Outlook.Application")
    
    Set newItem = olApp.CreateItemFromTemplate("C:\Users\user\Documents\newphones.oft")
    With newItem
        .Recipients.Add EmailAddr
        If .BodyFormat = 2 Then    ' 2 equ olFormatHTML
            .HTMLBody = VBA.Replace(.HTMLBody, PLACEHOLDER, FirstName)
        Else
            .Body = VBA.Replace(.Body, PLACEHOLDER, FirstName)
        End If
        .Display
    End With

    Set newItem = Nothing
    Set olApp = Nothing
End Sub
 
Solution

Trevor3007

Well-known Member
Joined
Jan 26, 2017
Messages
567
Hi GWteB,

Thanks for our help.

Got it to work.....FAB.

Have a great rest of your day.
 

Forum statistics

Threads
1,137,125
Messages
5,679,760
Members
419,855
Latest member
Eddier32

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
Top