简体   繁体   中英

VBA IF-Condition within an HTML-Body of an E-Mail

I have the following simple Excel spreadsheet:

    A       B
1   10
2   20

And I use the following VBA to send out an E-Mail:

Sub Test_EMail4()
    If ExitAll = False Then
        Dim OApp As Object, OMail As Object, signature As String
        Set OApp = CreateObject("Outlook.Application")
        Set OMail = OApp.CreateItem(0)
            With OMail
            .Display
            End With
            signature = OMail.HTMLbody
            With OMail
            .To = "test@test.de"
            .Subject = "test"
            .HTMLbody = "<p> Permant Content goes here </p>"
            If Sheet1.Range("A1").Value = 10 Then
            .HTMLbody = "<p> Content if Formula is true </p>"
            Else
            End If
            End With
        Set OMail = Nothing
        Set OApp = Nothing
    Else
    End If
End Sub

As you can see I have an If-condition within the HTML-Body. I want to achieve that the first tag <p> Permanet content goes here </p> is always shown in the E-Mail whereas the secont tag <p> Content if Formula is true </p> is only displayed if the condition in the IF-Formula is met (as in this case) .

Right now, it only shows the content within the IF-Formula in the E-Mail. How can I also include the permanent part?

Just build the message outside of the With OMail block like this:

Dim strBody As String

strBody = "<p> Permant Content goes here </p>"
If Sheet1.Range("A1").Value = 10 Then
    strBody = strBody & "<p> Content if Formula is true </p>"
End If

Then set strBody to the HTMLbody per your current code:

Sub Test_EMail4()

    Dim strBody As String
    Dim OApp As Object, OMail As Object, signature As String

    strBody = "<p> Permant Content goes here </p>"
    If Sheet1.Range("A1").Value = 10 Then
        strBody = strBody & "<p> Content if Formula is true </p>"
    End If

    If ExitAll = False Then
        Set OApp = CreateObject("Outlook.Application")
        Set OMail = OApp.CreateItem(0)
        With OMail
            .display
        End With
        signature = OMail.HTMLBody
        With OMail
            .To = "test@test.de"
            .Subject = "test"
            .HTMLBody = strBody
        End With
        Set OMail = Nothing
        Set OApp = Nothing
    Else
    End If

End Sub

from what you put this solves it. Removed the Signature since there is no need for that imo.

Sub Test_EMail4()
If ExitAll = False Then
    Dim OApp As Object, OMail As Object
    Set OApp = CreateObject("Outlook.Application")
    Set OMail = OApp.CreateItem(0)

        With OMail
        .Display
        .To = "test@test.de"
        .Subject = "test"
        If Sheet1.Range("A1").Value = 10 Then
        .HTMLBody = "<p> Permant Content goes here </p>" & .HTMLBody
        Else
        .HTMLBody = "<p> Content if Formula is true </p>" & .HTMLBody
        End If
        End With
        With OMail
        .Display 'change to "send" 
        End With

    Set OMail = Nothing
    Set OApp = Nothing
Else
End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM