0

I need to create a table upon clicking of a button on Access Form, which will be a body of an email sent to clients. I managed to do the table with the header format that I need, but I am unable to get the alternate rows coloring to work. Below is teh code linked to the button:

'''

`Private Sub TESTbtn_Click()
 Set oApp = CreateObject("Outlook.application")
 Set oMail = oApp.CreateItem(olMailItem)
 oMail.HTMLBody = "<html><head><style> table {font-family: arial, sans-serif;border-collapse: collapse;width: 100%;} td,th{border: 1px solid #dddddd;text-align: left;padding: 8px;} tr: nth-child(even){background-color: #eee;}tr:nth-child(odd) {background-color: #fff; color: white;}th {background-color: black;  color: white;}</style></head><body><table><tr><th>Company</th><th>Contact</th><th>Country</th></tr><tr><td>Alfreds Futterkiste</td><td>Maria Anders</td><td>Germany</td></tr><tr><td>Centro comercial Moctezuma</td><td>Francisco Chang</td><td>Mexico</td></tr><tr><td>Ernst Handel</td><td>Roland Mendel</td><td>Austria</td></tr><tr><td>Island Trading</td> <td>Helen Bennett</td><td>UK</td></tr><tr><td>Laughing Bacchus Winecellars</td><td>Yoshi Tannamuri</td><td>Canada</td></tr><tr><td>Magazzini Alimentari Riuniti</td><td>Giovanni Rovelli</td><td>Italy</td></tr></table></body></html>"
' "<html><head><style>table {width:100%;} table, th, td {border: 1px solid black;  border-collapse: collapse;} th, td {padding: 15px; text-align: left;} #t01 tr:nth-child(even) {background-color: #eee;} #t01 tr:nth-child(odd) {background-color: #fff;}#t01 th {background-color: black;  color: white;}</style></head><body><p>Dear reader,</p><p>Please find below information on the letter.</p>" & "<p>Any queries please let us know</p><p>Regards</p><TABLE id="t01"><TR><TD>Rating:</TD><TD>1</TD></TR><TR><TD>BIN:</TD><TD>1234567</TD></TR><TR><TD>Company name:</TD><TD>Something Ltd.</TD></TR><TR><TD>Status:<TD>New</TD></TABLE> "
 oMail.Subject = "Status on case with BIN nr: "
 oMail.To = "testemail@gmail.com"
 oMail.BodyFormat = 2
 oMail.Display
 Set oMail = Nothing
 Set oApp = Nothing
End Sub`

Below is the current result of the code:

enter image description here

Am I missing anything plz?

Thank you,

  • It could simply be that Outlook doesn't support this. Try saving .HTMLBody in a .html file and open with a browser. – Andre Jun 24 '21 at 15:06
  • In browser your code has alternate rows as white with no text showing. I tested code found at https://infoheap.com/html-table-with-alternate-color-rows/. It works in html file opened in browser but not for Outlook email. Had to replace embedded quotes with apostrophes. Suggest using line continuation and concatenation to make code more readable. – June7 Jun 24 '21 at 16:38
  • Does this answer your question? [Alternating Colors of Rows with HTML](https://stackoverflow.com/questions/26850890/alternating-colors-of-rows-with-html) – June7 Jun 24 '21 at 16:47
  • @Andre you are correct, it works fine with a browser but not within Outlook. Thanks for the help. – Anwar Mirza Jun 24 '21 at 16:47
  • @June7 thank you, and I am looking into it now. – Anwar Mirza Jun 24 '21 at 16:48

0 Answers0