0

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
3
  • Personally, I prefer using a stylesheet instead of using inline styles. It makes things a lot easier. Commented Apr 9, 2020 at 7:27
  • @braX maaan believe me I wish I knew how to incorporate, do you have any good link recommendations? Commented Apr 9, 2020 at 7:33
  • i gave you another answer, but I know you already picked the best one per your question using inline styles. Commented Apr 9, 2020 at 7:39

2 Answers 2

2

Try this simply

.HTMLBody = "<table><td style='width:" & tblWidth & "px; color:#4d4d4d; height=2px;'></td></table>"
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! I see now that the quotes go inside
Yes. To put a variable, surround it with double quotes and use the ampersand " & tblWidth & "
Technically, you are surrounding the literal parts of the string in quotes, and closing them to include a variable. But you can think of it the other way around if that helps.
2

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?

8 Comments

Nice! Thanks for this!
I forgot to ask, will this work with ID tags like #customHeader to then be used later in my HTML code like so: <td id='customHeader'>
yeah i don't see why not... td#customHeader {width:500px;}
Great, just tried it and so far it is working. Ill write some code up and post it on Code Review, would you mind reviewing?
i never go to code review, but glad to hear you have it working. with css there typically isnt too much to optimize (for speed) since it's not exactly a compiled language, but you may be able to use fewer lines in the end...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.