.HTMLBody中的IF语句

时间:2016-07-19 14:13:07

标签: html vba if-statement iif

用户将通过用户表单输入答案,然后将发送回复的电子邮件摘要。 userform中的几个问题是可选的。

在.HMTLBody部分,我希望某些回复能够以红色文字通过电子邮件发送(如果有人回答的话)(通常不是这样,所以我希望它很容易被发现)。

有没有办法使用条件语句格式化.HTMLBody部分中的某些字段? (IIF?)

以下示例("业务部门"字段可选)。

谢谢。

Dim EmailBody as String

EmailBody = "Unit Name: " & UCase(Me.ttbUnitName.Value) & “<br>” _
    & "Nation: " & UCase(Me.cbxNation.Value) & “<br>” _
    & "State: " & UCase(Me.cbxState.Value) & “<br>” _
    & "County: " & UCase(Me.cbxCounty.Value) & “<br>” _
    & "Business Unit: " & UCase(Me.ttbBusinessUnit.Value)

   With MItem
        .To = "123@fake.com"
        .CC = myEmail
        .DeferredDeliveryTime = SendAt
        .Subject = Me.Name & " Submission"
        .HTMLBody = EmailBody
   End With

2 个答案:

答案 0 :(得分:1)

.HTMLBody只是一个字符串(预计是HTML)。您可以在构建该字符串时使用if

所以你可以使用像这样的结构

Dim EmailBody as String

EmailBody = "Unit Name: " & UCase(Me.ttbUnitName.Value) & “<br>” _
    & "Nation: " & UCase(Me.cbxNation.Value) & “<br>” _
    & "State: " & UCase(Me.cbxState.Value) & “<br>” _
    & "County: " & UCase(Me.cbxCounty.Value) & “<br>”
    if Me.ttbBusinessUnit.Value <> "" then 'or whatever condition you have
       EmailBody = EmailBody & "<font color=""red"">Business Unit: " & UCase(Me.ttbBusinessUnit.Value) & "</font>"
    end if

   With MItem
        .To = "123@fake.com"
        .CC = myEmail
        .DeferredDeliveryTime = SendAt
        .Subject = Me.Name & " Submission"
        .HTMLBody = EmailBody
   End With

答案 1 :(得分:0)

像这样。

"<font style='color:" & iif(len(Me.ttbBusinessUnit.Value)>0,"red","black") & ";'>" & UCase(Me.ttbBusinessUnit.Value) & "</font>"