PP8
PP8

Reputation: 197

How can I implement HTML inline styling using variables in VBA?

I am using VBA to generate a new Outlook Email. My code works as expected when assigning a hard coded table width, however I would like to assign width: 500px; to a variable and use the variable throughout the body of my email.

How can I achieve this? I know we cannot use CSS with VBA as we're limited to a bare bones version of HTML via VBA / Outlook

CODE

Sub SendEmail
'shortened code
Dim tblWidth as double
tblWidth = 500

'i would like <td style = 'width:tblWidth px;...

With EmailMessage
    .To = "[email protected]"
    '...other email properties listed here
    .HTMLBody = "<table><td style='width:500px; color:#4d4d4d; height=2px;'></td></table>"

End With

End Sub

Upvotes: 0

Views: 1800

Answers (2)

braX
braX

Reputation: 11755

To use a stylesheet instead:

Just create one using a string and include it in your HTMLBody

Dim sStyleSheet as String
sStyleSheet = "<style> td {width:500px;} </style>" 

or to include your variable

sStyleSheet = "<style> td {width:" & tblWidth & "px;} </style>" 

See how you are just building a string?

Then include it in the HTML:

sHTML = "<table><tr><td> Hello World </td></tr></table>"
sStyleSheet = "<style> td {width:" & tblWidth & "px;} </style>" 
.HTMLBody = sStyleSheet & sHTML

Make sense?

Upvotes: 2

YasserKhalil
YasserKhalil

Reputation: 9568

Try this simply

.HTMLBody = "<table><td style='width:" & tblWidth & "px; color:#4d4d4d; height=2px;'></td></table>"

Upvotes: 2

Related Questions