我有以下简单的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
正如您所看到的,我在HTML-Body中有一个If条件。
我希望实现第一个标记<p> Permanet content goes here </p>
始终显示在电子邮件中,而secont标记<p> Content if Formula is true </p>
仅在满足IF公式中的条件时显示(如此情况下)
现在,它只显示电子邮件中IF-Formula中的内容。我怎样才能包括永久性部分?
答案 0 :(得分:0)
只需在With 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
然后根据您当前的代码将strBody
设置为HTMLbody
:
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
答案 1 :(得分:0)
从你所说的解决它。删除了Signature
,因为不需要那个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