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

时间:2015-12-10 10:53:58

标签: excel vba excel-vba outlook outlook-vba

我想在邮件正文中组合文本,我也需要使用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

1 个答案:

答案 0 :(得分:1)

您只需要重复使用已存储在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