Solved

Complex email sent from excel

Posted on 2016-10-18
8
63 Views
Last Modified: 2016-11-04
Can anyone tell me how to edit this code so I can enter a whole selection of cells throughout the email I wish to send. Currently I can only get the address and title and first line to come up, after that nothing I try will work.


Sub SendEMail()
   

    Dim OutApp As Outlook.Application
    Dim OutMail As Outlook.MailItem
    Dim strbody As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(olMailItem)

   

    On Error Resume Next
    With OutMail
        .To = Cells(ActiveCell.Row, 19)
        .CC = ""
        .BCC = ""
        .Subject = Cells(ActiveCell.Row, 12)
        .Body = Cells(ActiveCell.Row, 9)
       
       
        .Body = "Hi there" & vbNewLine & vbNewLine & _
              "This is line 1" & vbNewLine & _
              "This is line 2" & vbNewLine & _
              "This is line 3" & vbNewLine & _
              "This is line 4"

        'SendUsingAccount is new in Office 2007
        'Change Item(1)to the account number that you want to use
        .SendUsingAccount = OutApp.Session.Accounts.Item(2)
0
Comment
Question by:Sam Coombes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
8 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 41848511
What exactly do you want to appear in the email?
0
 

Author Comment

by:Sam Coombes
ID: 41848728
I need it to say dear -active cell 3, I would like to remind you that active cell 6, that this is due to expire on active cell 4  etc

Thank you
0
 
LVL 14

Accepted Solution

by:
frankhelk earned 500 total points
ID: 41851613
Hmm ... the property .Body is just a simple string, as far as I remember. In your code, when assigning

.Body = "Hi there" & (...)

you overwrite the previous assigned value from the statement

.Body = Cells(ActiveCell.Row, 9)

The second assignment should look like

.Body = .Body & vbcrlf & "Hi there" & (...)

to preserve that. Besides of that, the first assignment should read

.Body = Cells(ActiveCell.Row, 9).Value

because your statement assigns a Range object to a string property ... maybe that the implicit cast will get that right, but it's not good practice to rely on such constructs ... they might backfire later on when some update of VB won't permit that cast anymore or the cast leads to another result.
0
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

Author Comment

by:Sam Coombes
ID: 41852383
That's great thank you but how do I insert lines spaces. So for example

Dear Dr Doctor


Could you please etc
0
 
LVL 14

Expert Comment

by:frankhelk
ID: 41853398
That's no problem ... the term vbCrLf (a VB predefined constant) I've mentioned above would add a carriage return / line feed sequence. Add two, and you'll get a blank line in between. Add three, and ... I bet you've got the picture ;-)

P.S.: DON'T use that in header fields ...
1
 

Author Comment

by:Sam Coombes
ID: 41853516
Thank you very much thats great
0
 
LVL 14

Expert Comment

by:frankhelk
ID: 41853840
You're welcome ... I would be pleased to get the points for that solution ;-)
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
A list of top three free exchange EDB viewers that helps the user to extract a mailbox from an unmounted .edb file and get a clear preview of all emails & other items with just a single click on mailboxes.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question