[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5907
  • Last Modified:

VBA Outlook Email HTMLBody

Hi,

I am sending an e-mail though outlook by using VBA. However the formatting is not very good. I know I can use HTML to help format the text.

I wanted to know if it was possible to create a html table as I have 5 fields. Each field can contain strings of different lengths so at the moment it doesn't look very nice. Please see below an example. The first line is the headers the other rows are the actual data. What is the best way to display/format this data?


ISIN            Security                Field           Old Value               New Value



CA135087YF56            CANADIAN GOVT 4% 01/06/17               Moody LT Issue Rating           Aaa             B3              

CA135087YF56            CANADIAN GOVT 4% 01/06/17               SandP LT Issue Rating           AAA             CCC+            

AT0000383864            AUSTRIAN GOVERNMENT 6.25% 15/07/27              Moody LT Issue Rating           Aaa             CCC+  



Thanks,

M
0
mcs26
Asked:
mcs26
2 Solutions
 
Scott Fell, EE MVEDeveloperCommented:
With html email you need to style things old school with your table s and styles  need to be done inline and not via a style sheet.

http://www.w3schools.com/css/default.asp http://coboldinosaur.com/pages/table-art.html  (no javascript though)

    <td style="background-color:#C06; color:#FFFFFF; font-family:Verdana, Geneva, sans-serif; font-size:14px">Hi there world!</td>

or <td><span style=style="background-color:#C06; color:#FFFFFF; font-family:Verdana, Geneva, sans-serif; font-size:14px">Stuff to say</span></td>


I think styling the td's is the better choice.
0
 
Chris BottomleyCommented:
It all depends on how much effort you want to put into it ... for example via HTML the first two rows of your request could be generated by:

Sub html1()
Dim olkApp As Object
Dim olMailItem As Object

    Set olkApp = Application
    Set olMailItem = olkApp.CreateItem(0)
    With olMailItem
        .BodyFormat = 2
        .To = "xyz@abc.com"
        .Subject = "TBD - Subject"
        .HTMLBody = ""
        .HTMLBody = .HTMLBody & "<BR>"
        .HTMLBody = .HTMLBody & "<table BORDER=1 WIDTH=80%>"
        .HTMLBody = .HTMLBody & "<tr>"
        .HTMLBody = .HTMLBody & "<td WIDTH=15%>"
        .HTMLBody = .HTMLBody & "ISIN"
        .HTMLBody = .HTMLBody & "</td>"
        .HTMLBody = .HTMLBody & "<td WIDTH=25%>"
        .HTMLBody = .HTMLBody & "Security"
        .HTMLBody = .HTMLBody & "</td>"
        .HTMLBody = .HTMLBody & "<td WIDTH=25%>"
        .HTMLBody = .HTMLBody & "Field"
        .HTMLBody = .HTMLBody & "</td>"
        .HTMLBody = .HTMLBody & "<td WIDTH=15%>"
        .HTMLBody = .HTMLBody & "Old Value"
        .HTMLBody = .HTMLBody & "</td>"
        .HTMLBody = .HTMLBody & "<td WIDTH=15%>"
        .HTMLBody = .HTMLBody & "New Value"
        .HTMLBody = .HTMLBody & "</td>"
        .HTMLBody = .HTMLBody & "</tr>"
        
        .HTMLBody = .HTMLBody & "<tr>"
        .HTMLBody = .HTMLBody & "<td>"
        .HTMLBody = .HTMLBody & "CA135087YF56"
        .HTMLBody = .HTMLBody & "</td>"
        .HTMLBody = .HTMLBody & "<td>"
        .HTMLBody = .HTMLBody & "CANADIAN GOVT 4% 01/06/17"
        .HTMLBody = .HTMLBody & "</td>"
        .HTMLBody = .HTMLBody & "<td>"
        .HTMLBody = .HTMLBody & "Moody LT Issue Rating"
        .HTMLBody = .HTMLBody & "</td>"
        .HTMLBody = .HTMLBody & "<td>"
        .HTMLBody = .HTMLBody & "Aaa"
        .HTMLBody = .HTMLBody & "</td>"
        .HTMLBody = .HTMLBody & "<td>"
        .HTMLBody = .HTMLBody & "CCC+"
        .HTMLBody = .HTMLBody & "</td>"
        .HTMLBody = .HTMLBody & "</tr>"
        
        .HTMLBody = .HTMLBody & "</table>"
        .Display
    End With

End Sub

Open in new window


You can of course tweak the parameters and the second row can be cloned to create multiple rows.

Chris
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now