如何在使用vba的邮件正文中连接文本,当我们需要使用IF THEN条件?

我想结合邮件正文中的文本,也需要使用IFTHEN条件。 我应该如何继续。 下面给出的是代码,可以看到Body之间的If条件。

Dim olApp As Object Set olApp = CreateObject("Outlook.Application") Dim olMail As Object Set olMail = olApp.CreateItem(olMailItem) Dim x As Integer Dim i As Integer Dim last As Integer x = 3 i = 2 last = Range("H" & Rows.Count).End(xlUp).Row Cells(23, 2).Value = last Do While i <= last If x - i = 1 Then olMail.To = Cells(i, 8).Value & ";" & Cells(i, 9).Value olMail.Subject = "Subject" olMail.Body = "Document details as follows:" & vbNewLine & _ "Title : " & Cells(i, 1).FormulaR1C1 & vbNewLine & _ "EP Number : " & Cells(i, 5).FormulaR1C1 & vbNewLine & _ "Product Type : " & Cells(i, 4).FormulaR1C1 & vbNewLine & _ "Well(s) : " & Cells(i, 2).FormulaR1C1 & vbNewLine & _ "Field(s) : " & Cells(i, 3).FormulaR1C1 & vbNewLine & vbNewLine & _ "Straight Link to the document : " & Cells(i, 7).FormulaR1C1 & _ If Cells(i, 8).Value = Cells(x, 8).Value Then "Document details as follows:" & vbNewLine & _ "Title : " & Cells(x, 1).FormulaR1C1 & vbNewLine & _ "EP Number : " & Cells(x, 5).FormulaR1C1 & vbNewLine & _ "Product Type : " & Cells(x, 4).FormulaR1C1 & vbNewLine & _ "Well(s) : " & Cells(x, 2).FormulaR1C1 & vbNewLine & _ "Field(s) : " & Cells(x, 3).FormulaR1C1 & vbNewLine & vbNewLine & _ "Straight Link to the document : " & Cells(x, 7).FormulaR1C1 x = x + 1 End If Else olMail.Send i = x x = x + 1 End If Loop 

你只需要重用你已经存储在身体里面的If

 Dim olApp As Object Set olApp = CreateObject("Outlook.Application") Dim olMail As Object Set olMail = olApp.CreateItem(olMailItem) Dim x As Integer Dim i As Integer Dim last As Integer x = 3 i = 2 last = Range("H" & Rows.Count).End(xlUp).Row Cells(23, 2).Value = last Do While i <= last If x - i = 1 Then olMail.To = Cells(i, 8).Value & ";" & Cells(i, 9).Value olMail.Subject = "Subject" olMail.Body = _ "Document details as follows:" & vbNewLine & _ "Title : " & Cells(i, 1).FormulaR1C1 & vbNewLine & _ "EP Number : " & Cells(i, 5).FormulaR1C1 & vbNewLine & _ "Product Type : " & Cells(i, 4).FormulaR1C1 & vbNewLine & _ "Well(s) : " & Cells(i, 2).FormulaR1C1 & vbNewLine & _ "Field(s) : " & Cells(i, 3).FormulaR1C1 & vbNewLine & vbNewLine & _ "Straight Link to the document : " & Cells(i, 7).FormulaR1C1 If Cells(i, 8).Value = Cells(x, 8).Value Then olMail.Body = olMail.Body & vbNewLine & _ "Document details as follows:" & vbNewLine & _ "Title : " & Cells(x, 1).FormulaR1C1 & vbNewLine & _ "EP Number : " & Cells(x, 5).FormulaR1C1 & vbNewLine & _ "Product Type : " & Cells(x, 4).FormulaR1C1 & vbNewLine & _ "Well(s) : " & Cells(x, 2).FormulaR1C1 & vbNewLine & _ "Field(s) : " & Cells(x, 3).FormulaR1C1 & vbNewLine & vbNewLine & _ "Straight Link to the document : " & Cells(x, 7).FormulaR1C1 x = x + 1 End If Else olMail.Send i = x x = x + 1 End If Loop