1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

E-mail VBA code

Discussion in 'Business Applications' started by mpaculea, Jan 29, 2015.

Thread Status:
Not open for further replies.
  1. mpaculea

    mpaculea Thread Starter

    Joined:
    Jan 29, 2015
    Messages:
    1
    Hello, I use this code designed by one member of this forum named XCubed the only problem I have is that I would like to have more font formating options to the email body to create a more complex email body.

    What I would exactly like is an option to let me add HTML Tags, <b> , </br> , <p> and other tags.

    If its not possible atleast I would like the option to bold a part of the text lets say to bold the "Please update" and to add a background color to the word "Please"

    Thank you very much and I hope i was clear enough.

    Code:
    Sub eMail()
    Dim lRow As Integer
    Dim i As Integer
    Dim toDate As Date
    Dim toList As String
    Dim eSubject As String
    Dim eBody As String
    
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .DisplayAlerts = False
    End With
    
    Sheets(1).Select
    lRow = Cells(Rows.Count, 4).End(xlUp).Row
    
    For i = 2 To lRow
    toDate = Replace(Cells(i, 3), ".", "/")
      If Left(Cells(i, 5), 4) <> "Mail" And toDate - Date <= 7 Then
         Set OutApp = CreateObject("Outlook.Application")
         Set OutMail = OutApp.CreateItem(0)
    
            toList = Cells(i, 4)    'gets the recipient from col D
            eSubject = "Project " & Cells(i, 2) & " is due on " & Cells(i, 3)
            eBody = "Dear " & Cells(i, 1) "Please update your project status."
            
            On Error Resume Next
            With OutMail
            .To = toList
            .CC = ""
            .BCC = ""
            .Subject = eSubject
            .Body = eBody
            .bodyformat = 1
            .Display   ' ********* Creates draft emails. Comment this out when you are ready
            '.Send     '********** UN-comment this when you  are ready to go live
            End With
     
        On Error GoTo 0
        Set OutMail = Nothing
        Set OutApp = Nothing
     Cells(i, 5) = "Mail Sent " & Date + Time 'Marks the row as "email sent in Column A"
    End If
    Next i
    
    ActiveWorkbook.Save
    
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .DisplayAlerts = True
    End With
    End Sub
    
    
     
  2. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    You just need to format the body as "olFormatHTML" and then compose the olEmail.HTMLBody to include your actual text along with the HTML tags. See my example below

    Code:
    Dim olApp As Outlook.Application
    
    Dim olEmail As Outlook.MailItem
    
     
    
    Set olApp = New Outlook.Application
    
    Set olEmail = olApp.CreateItem(olMailItem)
    
     
    
    olEmail.BodyFormat = olFormatHTML
    
    olEmail.HTMLBody = "<font face='comic sans ms' color='red'>THIS IS A TEST</font>"
    
    olEmail.Display
    
     
    
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1142094

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice