2015-12-10 45 views
0

我想結合郵件正文中的文本,也需要使用IFTHEN條件。我應該如何繼續。下面給出的是代碼,你可以看到Body之間的If條件。如何在我們需要使用IF THEN條件時使用vba連接郵件正文中的文本?

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

你只需要重用你已經儲存在體內的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 
相關問題