Использование Excel VBA, ЕСЛИ ТО ЗАПИСЬ в теле письма - PullRequest
0 голосов
/ 06 марта 2020

Я пытаюсь использовать оператор if then внутри .body моего письма, но когда я его запускаю, он ничего не делает. Ниже, где я заметил, тело письма начинается, где я застрял.

    Sub Email_From_Excel_Basic()

' This identifies what means what
    Dim emailApplication As Object
    Dim emailItem As Object
    Dim mymsg As String
    Dim cell As Range

    Application.ScreenUpdating = False
    Set emailApplication = CreateObject("Outlook.Application")

' Now we build the email.
    On Error GoTo cleanup
    For Each cell In Worksheets("owvr").Columns("S").Cells
        Set emailItem = emailApplication.CreateItem(0)
        If cell.Value Like "?*@?*.?*" And _
            Cells(cell.Row, "T") = "Yes" Then
                With emailItem
' This is who the email is being sent to
                    .To = Cells(cell.Row, "S").Value
                    .CC = Cells(cell.Row, "S").Value
' This is the subject of the email
                    .Subject = "Status update"
' This is the body of the email based on cell references
                    mymsg = "Dear " & Cells(cell.Row, "A").Value & " team," & vbNewLine & vbNewLine

Здесь

                    mymsg = mymsg & "Status: " & If Cell("D").Value = "1. New Order" Then
                    mymsg "Your order has been received and will processed."
                    ElseIf cell("D").Value= "2. Shipped" Then
                    mymsg = mymsg & "Status: Your order has been shipped" & vbNewLine
                    Else
                    End If
                    mymsg = mymsg & "Deposit invoice: " & Cells(cell.Row, "K").Value & vbNewLine
                    mymsg = mymsg & "Additional invoice: " & Cells(cell.Row, "M").Value & vbNewLine
                    .Body = mymsg
                    .Send
' This part brings the loop back up to the top, so this way it goes to the next email in the column
                     End With
                On Error GoTo 0
                Set emailItem = Nothing
            End If
        Next cell

    cleanup:
        Set emailApplication = Nothing
        Set emailItem = Nothing
        Set mymsg = Nothing
        Application.ScreenUpdating = True
    End Sub

Совет?

1 Ответ

2 голосов
/ 06 марта 2020

Пусть IF заполнит переменную строку, а затем объединит ее в теле.

Sub Email_From_Excel_Basic()

    ' This identifies what means what
    Dim emailApplication As Object
    Dim emailItem As Object
    Dim mymsg As String
    Dim cell As Range

    Application.ScreenUpdating = False
    Set emailApplication = CreateObject("Outlook.Application")

    ' Now we build the email.
    On Error GoTo cleanup
    For Each cell In Worksheets("owvr").Columns("S").Cells
        Set emailItem = emailApplication.CreateItem(0)
        If cell.Value Like "?*@?*.?*" And _
           Cells(cell.Row, "T") = "Yes" Then
            With emailItem
                ' This is who the email is being sent to
                .To = Cells(cell.Row, "S").Value
                .CC = Cells(cell.Row, "S").Value
                ' This is the subject of the email
                .Subject = "Status update"
                ' This is the body of the email based on cell references
                mymsg = "Dear " & Cells(cell.Row, "A").Value & " team," & vbNewLine & vbNewLine
                Dim stts As String
                If Cells(cell.Row, 4).Value = "1. New Order" Then
                    stts = "Your order has been received and will processed."
                ElseIf Cells(cell.Row, 4).Value = "2. Shipped" Then
                    stts "Status: Your order has been shipped" & vbNewLine
                Else
                End If
                mymsg = mymsg & "Status: " & stts

                mymsg = mymsg & "Deposit invoice: " & Cells(cell.Row, "K").Value & vbNewLine
                mymsg = mymsg & "Additional invoice: " & Cells(cell.Row, "M").Value & vbNewLine
                .Body = mymsg
                .Send
                ' This part brings the loop back up to the top, so this way it goes to the next email in the column
            End With
            On Error GoTo 0
            Set emailItem = Nothing
        End If
    Next cell

    cleanup:
    Set emailApplication = Nothing
    Set emailItem = Nothing
    Set mymsg = Nothing
    Application.ScreenUpdating = True
End Sub

...