电子邮件HTML正文中的VBA IF条件

电子邮件HTML正文中的VBA IF条件,html,css,excel,vba,Html,Css,Excel,Vba,我有以下简单的Excel电子表格: A B 1 10 2 20 我使用以下VBA发送电子邮件: Sub Test_EMail4() If ExitAll = False Then Dim OApp As Object, OMail As Object, signature As String Set OApp = CreateObject("Outlook.Application") Set OMail = O

我有以下简单的Excel电子表格:

    A       B
1   10
2   20
我使用以下VBA发送电子邮件:

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
子测试_EMail4()
如果ExitAll=False,则
Dim OApp作为对象,OMail作为对象,签名作为字符串
设置OApp=CreateObject(“Outlook.Application”)
设置OMail=OApp.CreateItem(0)
与奥马尔
.展示
以
签名=OMail.HTMLbody
与奥马尔
.To=”test@test.de"
.Subject=“测试”
.HTMLbody=“永久内容在此处出现

” 如果表1.范围(“A1”).值=10,则 .HTMLbody=“公式为真时的内容”

“ 其他的 如果结束 以 设置OMail=Nothing 设置OApp=Nothing 其他的 如果结束 端接头
正如您所看到的,我在HTML主体中有一个If条件。 我想实现的是,第一个标记
永久网内容始终显示在电子邮件中,而secont标记
内容if Formula is true

仅在满足if公式中的条件时显示(如本例所示)


现在,它只在电子邮件中显示IF公式中的内容。我如何也包括永久性部件?

只需使用OMail
块在
之外构建消息,如下所示:

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

从你所说的,这解决了它。删除了
签名
,因为不需要该签名

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
子测试_EMail4()
如果ExitAll=False,则
Dim OApp作为对象,OMail作为对象
设置OApp=CreateObject(“Outlook.Application”)
设置OMail=OApp.CreateItem(0)
与奥马尔
.展示
.To=”test@test.de"
.Subject=“测试”
如果表1.范围(“A1”).值=10,则
.HTMLBody=“永久内容在此处

”&.HTMLBody 其他的 .HTMLBody=“公式为真时的内容

”&.HTMLBody 如果结束 以 与奥马尔 .显示“更改为“发送” 以 设置OMail=Nothing 设置OApp=Nothing 其他的 如果结束 端接头
嗨,罗宾,当我在VBA中复制这两个代码时,我必须考虑一下吗?当我在VBA中键入它们时,我的电子邮件中没有显示任何内容。请参阅我的编辑-希望有帮助-在完整的
Sub
Hi-Robin中包含您原始代码的建议,当我输入您的代码时,我收到错误“Object required(424)”。我试着放一个潜艇()。。。在第一个代码周围结束Sub(),但它仍然不起作用。你知道我的错误在哪里吗?嗨,米奇-第一个代码块只是一个例子。其目的只是让您粘贴到第二个块中,从
Sub Test\u EMail4()
End Sub
。我在
Test\u Email4()
sub中包含了设置电子邮件正文的代码。您在哪一行看到错误?嗨,Robin,我犯了一个错误,因为我必须用另一个数字替换Excel电子表格中的Sheet1。现在它工作得很好。谢谢。
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