Solved

Complex email sent from excel

Posted on 2016-10-18
8
50 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
  • 3
  • 3
8 Comments
 
LVL 33

Expert Comment

by:Norie
Comment Utility
What exactly do you want to appear in the email?
0
 

Author Comment

by:Sam Coombes
Comment Utility
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 13

Accepted Solution

by:
frankhelk earned 500 total points
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

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

Dear Dr Doctor


Could you please etc
0
 
LVL 13

Expert Comment

by:frankhelk
Comment Utility
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
Comment Utility
Thank you very much thats great
0
 
LVL 13

Expert Comment

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

Featured Post

Do email signature updates give you a headache?

Constantly trying to correctly format email signatures? Spending all of your time at every user’s desk to make updates? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today!

Join & Write a Comment

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
Resolve DNS query failed errors for Exchange
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now