Help with code - Inserting values from cells in a spreadsheet in an email message.

Jay3

Board Regular
Joined
Jul 3, 2009
Messages
237
Hi Everyone,

This should be a relitively straight forward question for anyone who know VBA.

I need to amend the following code so that when the email is composed but inserts values from cells in the spreadsheet?

Can anyone tell me how to do this?

Code:
Sub Mail_small_Text_Outlook()
'Working in Office 2000-2010
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    strbody = "Hi there" & vbNewLine & vbNewLine & _
              "Cell A1 is changed" & vbNewLine & _
              "This is line 2" & vbNewLine & _
              "This is line 3" & vbNewLine & _
              "This is line 4"
    On Error Resume Next
    With OutMail
        .To = "[EMAIL="emailaddresshere@somewher.co.uk"]emailaddresshere@somewher.co.uk[/EMAIL]"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .Body = strbody
        'You can add a file like this
        '.Attachments.Add ("C:\test.txt")
        .Display   'or use .Send
    End With
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub


The bit of the code I need to amend is shown below. I want to be able to insert the value from B2, B3, B4 for example.


Code:
 "Cell A1 is changed" & vbNewLine & _
              "This is line 2" & vbNewLine & _
              "This is line 3" & vbNewLine & _
              "This is line 4"


Can someone also tell me how to get the email to send automatically rather than just be drafted for the user to then hit send?

Many thanks,
Jay3:biggrin:
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try

Code:
strbody = "Hi there" & vbNewLine & vbNewLine & _
              "Cell A1 is changed" & vbNewLine & _
              Range("B2").Value & vbNewLine & _
              Range("B3").Value & vbNewLine & _
              Range("B4").Value

And change .Display to .Send.
 
Upvote 0
Hi....thanks for that really helpful.

Can you tell me what to do if I want to concatenate.

For example.....


"A new request has been added to the list by [B2] the deadline is [C2]" etc...

Thanks,
Jay3
 
Upvote 0
Assuming that C2 contains a date try like this

Code:
strBody = "A new request has been added to the list by " & Range("B2").Value & " the deadline is " & Format(Range("C2").Value, "dd/mm/yyyy")
 
Upvote 0
I need to amend the following code so that when the email is composed but inserts values from cells in the spreadsheet?

Ah, I emailed you last night (11pm UK time, it's 9.30am here now).

Can someone also tell me how to get the email to send automatically rather than just be drafted for the user to then hit send?
Code:
        .Display [COLOR=green] 'or use .Send[/COLOR]
:)
 
Upvote 0
Thanks this is really helpful!

As this is a task list can you help be tweak the code so that it looks at the most recently added row each time?

For eaxmple if someone adds a new task on row 3 I will need the cell reference to update accordingly.

This is the code so far.

Code:
Sub Mail_small_Text_Outlook()
'Working in Office 2000-2010
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    strbody = "Dear John" & vbNewLine & vbNewLine & _
              "A new request has been added to the list by " & Range("B2").Value & _
              " the aspirational deadline is " & Format(Range("E2").Value, "dd/mm/yyyy") & _
              " the external deadline is " & Format(Range("F2").Value, "dd/mm/yyyy") & vbNewLine & _
              " Request Title: " & Range("C2").Value & vbNewLine & _
              " Request Description: " & Range("D2").Value
              
                           
    On Error Resume Next
    With OutMail
        .To = "[EMAIL="emailaddresshere@somewher.co.uk"]emailaddresshere@somewher.co.uk[/EMAIL]"
        .CC = ""
        .BCC = ""
        .Subject = "New Task Added to Request Listing Today"
        .Body = strbody
        'You can add a file like this
        '.Attachments.Add ("C:\test.txt")
        .Display   'or use .Send
    End With
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

Anyhelp is much appreciated.

Thanks,
Jay3
 
Upvote 0
Try

Code:
Sub Mail_small_Text_Outlook()
'Working in Office 2000-2010
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String
    Dim LR As Long
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    LR = Range("B" & Rows.Count).End(xlUp).Row
    strbody = "Dear John" & vbNewLine & vbNewLine & _
              "A new request has been added to the list by " & Range("B" & LR).Value & _
              " the aspirational deadline is " & Format(Range("E" & LR).Value, "dd/mm/yyyy") & _
              " the external deadline is " & Format(Range("F" & LR).Value, "dd/mm/yyyy") & vbNewLine & _
              " Request Title: " & Range("C" & LR).Value & vbNewLine & _
              " Request Description: " & Range("D" & LR).Value
              
                           
    On Error Resume Next
    With OutMail
        .To = "emailaddresshere@somewher.co.uk"
        .CC = ""
        .BCC = ""
        .Subject = "New Task Added to Request Listing Today"
        .Body = strbody
        'You can add a file like this
        '.Attachments.Add ("C:\test.txt")
        .Send
    End With
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,552
Messages
6,179,484
Members
452,917
Latest member
MrsMSalt

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