Solved

Complex email sent from excel

Posted on 2016-10-18
8
55 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
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

Too many email signature changes to deal with?

Are you constantly being asked to update your organization's email signatures? Do they take up too much of your time? Wouldn't you love to be able to manage all signatures from one central location, easily design them and deploy them quickly to users. Well, you can!

Question has a verified solution.

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

Exchange server is not supported in any cloud-hosted platform (other than Azure with Azure Premium Storage).
Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

11 Experts available now in Live!

Get 1:1 Help Now