如果Excel单元格中满足条件,则发送Outlook电子邮件

时间:2019-03-15 12:12:49

标签: excel vba outlook outlook-vba

我需要使用Excel发送带有两个条件的电子邮件。

  • 单元格D2必须大于等于1
  • 单元格E2必须为=“”。

我已经完成了第一个条件,但是没有第二个条件

代码是:

'PRAZO Etapa 4
Public Sub EnviarEmailEt4()    

Dim OutApp As Object
Dim OutMail As Object
Dim Body As String

  Worksheets("Incidentes2019").Select
  Range("D4").Select
  Do While ActiveCell.Value <> ""
     If ActiveCell >= 1 And ActiveCell.Offset(0, 1) = "" And InStr(2, Cells(ActiveCell.Row, 10), "@") > 0 Then

        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)

        With OutMail
             .To = Cells(ActiveCell.Row, 10).Value
             .CC = Cells(ActiveCell.Row, 11).Value
             .BCC = ""
             .Subject = Cells(ActiveCell.Row, 3).Value
            If (ActiveCell = 1) Or (ActiveCell = 2) Then
                .Body = "ALERTA PRAZO ETAPA 4!!" & vbNewLine & vbNewLine & "Nº GQE " & Cells(ActiveCell.Row, 2).Value & " - " & Cells(ActiveCell.Row, 3).Value
            ElseIf (ActiveCell >= 3) Then
                .Body = "ULTRAPASSADO PRAZO ETAPA 4!!" & vbNewLine & vbNewLine & "Nº GQE " & Cells(ActiveCell.Row, 2).Value & " - " & Cells(ActiveCell.Row, 3).Value
            End If
            .Send 'Display

        End With
     Set OutMail = Nothing
     Set OutApp = Nothing
     MsgBox "Alerta Etapa 4 enviado - " & Format(Now, "HH:MM") & vbNewLine & Cells(ActiveCell.Row, 3).Value
     End If

    Cells(ActiveCell.Row + 1, ActiveCell.Column).Select
    Loop
End Sub

2 个答案:

答案 0 :(得分:1)

尝试此操作,您可以在邮件过程之外检查循环,如果单元格符合您的条件,则可以发送邮件:

Option Explicit
Sub SendingMails()

    Dim ws As Worksheet 'always declare worksheets and workbooks to avoid using select
    Dim SendTo As String, SendSubject As String, FirstData As String, SecondData As String 'here, variables for the items to fill on your mail
    Dim LastRow As Long, i As Long 'Here you have the lastrow of the worksheet and another variable for a loop

    Set ws = ThisWorkbook.Sheets("Sheet1") 'Change Sheet1 for the name of the sheet where you keep the data
    With ws
        LastRow = .Cells(.Rows.Count, 4).End(xlUp).Row 'this will check the last row with data on the column 4 (D)
        For i = 2 To LastRow 'starting from row 2 to the last one with data
            If .Cells(i, 4) >= 1 And .Cells(i, 5) <> vbNullString Then 'here you check if column D cell has 1 or higher and if column E cell is empty
                SendTo = .Cells(i, 10)
                SendSubject = .Cells(i, 3)
                FirstData = .Cells(i, 2)
                SecondData = .Cells(i, 3)
                Call EnviarEmailEt4(SendTo, SendSubject, FirstData, SecondData)
            End If
        Next i
    End With


End Sub
Sub EnviarEmailEt4(SendTo As String, SendSubject As String, FirstData As String, SecondData As String)

    'as you can see above, i've declared variables inside the procedure which will be taken from the previous one

    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
        .to = SendTo
        .CC = ""
        .BCC = ""
        .Subject = SendSubject
        .Body = "ALERTA FIM DE PRAZO ETAPA 4!!" & vbNewLine & vbNewLine & "Nº GQE " & FirstData & " - " & SecondData
        '.Attachments.Add ActiveWorkbook.FullName 'Anexar este ficheiro
        '.Attachments.Add ("") 'Anexar outro ficheiro
        .send 'Display
    End With
    Set OutMail = Nothing
    Set OutApp = Nothing
    MsgBox "     Alerta Et4 enviado - " & Format(Now, "HH:MM") 'I Would avoid alerting in each loop if there are lots of mails

End Sub

答案 1 :(得分:0)

您可以使用if len(max(teststring.split(' '), key=len)) == 1: print("is separated") else: print("is not separated") 属性选择右侧的单元格(如按excel中的箭头)。尝试将您的IF语句更改为以下内容:

var MyType = function(_param1,_param2){
this.param1 = _param1;
this.param2 = _param2;

this.ShowParam = function(){
  alert(this.param1+" - "+this.param2);
  }
}

var test = new MyType("PARAM TEST 1","PARAM TEST 2");

alert(test.param1+" - "+test.param2);


var test2 = new MyType("PARAM TEST 1.2","PARAM TEST 2.2");

alert(test2.param1+" - "+test2.param2);

test.ShowParam();
test2.ShowParam();

 

编辑: 响应您的问题更改:这是一种基于activecell值设置邮件正文的有效方法:

Range.offset()
相关问题