Using VBA If In HTML Body

TigerTom

New Member
Joined
Jun 20, 2017
Messages
12
Hi All,

As part of a bigger macro, I'm trying to get the macro to write and e-mail and I want the e-mail content to depend on a cell value in a worksheet.

I.e If cell is greater than 5, "no need to re-order", if cell is less than 5, "please place an order.

This is my test code below. I can get it to compile, probably a silly syntax error somewhere.

Any help gratefully received.

Sub IfTest()


Code:
Windows("Test.xlsx").Activate


Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")
'
Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)
'
olMail.To = "test@test.com;"
olMail.Subject = "Test"
olMail.HTMLBody = "****** style=font-size:11pt;font-family:Calibri>" & _
"<p>If test below</p>" & _
"<p></p>" & _
If Range("A23") < 5 Then
("<p>Some e-mail text.</p>")
Else
("<p>Some other e-mail text.</p>")
End If
"</BODY>"


End Sub

Thanks,

Tiger
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Sorry hope this is a bit clearer
Sub IfTest()


Windows("Test.xlsx").Activate


Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")
'
Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)
'
olMail.To = "test@test.com;"
olMail.Subject = "Test"
olMail.HTMLBody = "****** style=font-size:11pt;font-family:Calibri>" & _
"<p>If test below</p>" & _
"<p></p>" & _
If Range("A23") < 5 Then
("<p>Some e-mail text.</p>")
Else
("<p>Some other e-mail text.</p>")
End If
"</BODY>"


End Sub
 
Upvote 0
Perhaps.
Code:
olMail.HTMLBody = "****** style=font-size:11pt;font-family:Calibri>" & _
    "HTML stuff " & _
    IIf(Range("A23") < 5, "Some e-mail text.", "Some other e-mail text.") & _
    "More HTML Stuff"
 
Upvote 0
Thank you Norie, that would work.?

If I wanted to make a list of these can I have a go to next if the result is false?

Or should I just put the false part in double "" to leave it blank.

Thanks again,

Tiger.
 
Upvote 0
Sorry didn't explain that we'll.

I have 15 cells I need to perform the same if test on, flagging up any true results in the email.

Was wondering what was cleanest way to have false result empty and move onto next if statement.

Tiger.
 
Upvote 0
Do IF formulas in adjacent cells and pull the result?

=IF(A23<5, "Some e-mail text", "Some other e-mail text")
 
Upvote 0

Forum statistics

Threads
1,223,315
Messages
6,171,410
Members
452,399
Latest member
oranges

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top