Macro to insert text from cells in body in email

KlausW

Active Member
Joined
Sep 9, 2020
Messages
378
Office Version
  1. 2016
Platform
  1. Windows
Hi

Someone who can help, I use this VBA Code to send mail. It works well. Now I would like to be able to write mails body in some cells range I3, I9 and I11, so I can change it from time to time.

All help will be appreciated.
Best regards Klaus

Code:
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Sorry I did not get the code above, but here it is. KW
VBA Code:
Sub Mail_workbook_Outlook()
'her er koden til at sende mail
    Dim Edress As String, Subj As String
    Dim OutlookOBJ As Object, mItem As Object
    '---------------------------------------------'
    Set OutlookOBJ = CreateObject("Outlook.Application")
    Set mItem = OutlookOBJ.CreateItem(olMailItem)
    With mItem
    
        .To = Sheets("Bestilling").Range("i1").Value
        .CC = ""
        .BCC = ""
        .Subject = Sheets("Bestilling").Range("a1").Value
       
        .Body = "Hej Bo" & vbNewLine & _
                " " & vbNewLine & _
              "Vil du bestille dette for mig." & vbNewLine & _
                " " & vbNewLine & _
                "Med venlig hilsen" & vbNewLine & _
               " " & vbNewLine & _
                "Klaus Wittusen" & vbCrLf & "Oversergent" & vbCrLf & "Proviantbefalingsmand" & vbCrLf & "Kongeskibet Dannebrog"
        
        '.Send                               '<-- .Send will auto send email without review
                                
 ThisWorkbook.Save
        .Attachments.Add ThisWorkbook.Path & "\" & ThisWorkbook.Name
        .Send
    End With

End Sub
 
Upvote 0
Simply replace the text with the cell values you want as in the code below.

VBA Code:
.Body = Range(I3).Value & vbNewLine & _
                " " & vbNewLine & _
              Range(I9).Value & vbNewLine & _
                " " & vbNewLine & _
                Range(I11).Value & vbNewLine & _
               " " & vbNewLine & _
                "Klaus Wittusen" & vbCrLf & "Oversergent" & vbCrLf & "Proviantbefalingsmand" & vbCrLf & "Kongeskibet Dannebrog"
 
Upvote 0
Solution
hi again i changed code to. .Body = Range ("I3") .Value then Excel takes the text from cell I3. but I can not get it to take the other cells. KW
 
Upvote 0
You may need to create the "body" in a string variable before setting it in the body

something like

VBA Code:
Dim sBody as string

sBody = Range("I3").Value & vbNewLine & _
                " " & vbNewLine & _
              Range("I9").Value & vbNewLine & _
                " " & vbNewLine & _
                Range("I11").Value & vbNewLine & _
               " " & vbNewLine & _
                "Klaus Wittusen" & vbCrLf & "Oversergent" & vbCrLf & "Proviantbefalingsmand" & vbCrLf & "Kongeskibet Dannebrog"

then set the body using

VBA Code:
.Body = sBody
 
Upvote 0
Good evening the previous solution seemed 110% thanks of the help, have a nice day KW
 
Upvote 0

Forum statistics

Threads
1,214,422
Messages
6,119,395
Members
448,891
Latest member
tpierce

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