Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Complex email sent from excel

Posted on 2016-10-18
8
Medium Priority
?
69 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 34

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 2000 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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Check out this step-by-step guide for using the newly updated Experts Exchange mobile app—released on May 30.
New style of hardware planning for Microsoft Exchange server.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

661 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