简体   繁体   English

在excel上分散的电子邮件列表,如何每人发送一封电子邮件? 以及如何在电子邮件正文中包含单元格的内容

[英]scattered email list on excel, how to send one email per person? And how to include content of a cell in email body

So I have an excel sheet organized by case that are assigned to emails. 因此,我有一个按大小写组织的excel表,分配给电子邮件。 Each case is assigned to one email and each email is responsible for more than one case. 每个案例都分配给一封电子邮件,每封电子邮件负责一个以上的案例。 Emails are not in order, they are scattered throughout the column. 电子邮件不整齐,它们分散在整个列中。 I want to create an automated email that sends a reminder every Monday (this I havent figured out how yet) to submit the case. 我想创建一个自动电子邮件,该电子邮件在每个星期一发送提醒(我还没有弄清楚如何)以提交案件。 Problem is I want to send one email per person regrouping all the cases assigned to them that are due. 问题是我想每人发送一封电子邮件,将分配给他们的所有案件重新分组。 (When a case is closed it disappears from the sheet so no need to worry about this). (当箱子关闭时,它会从工作表中消失,因此无需担心)。

Here's what I already wrote: 这是我已经写的:

    Sub datesexcelvba()

Dim myApp As Outlook.Application, mymail As Outlook.MailItem
Dim mydate1 As Date
Dim mydate2 As Long  
Dim datetoday1 As Date
Dim datetoday2 As Long
Dim engineer As Range

Dim x As Long
lastrow = Sheets("Messages english").Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To lastrow

mydate1 = Cells(x, 3).Value
mydate2 = mydate1
Cells(x, 7) = mydate2

datetoday1 = Date
datetoday2 = datetoday1


Cells(x, 9).Value = datetoday2
Set daysLeft = mydate2 - datetoday2

Function itsokay()
If daysLeft <= 14 And daysLeft >= 8 Then
Set myApp = New Outlook.Application
Set mymail = myApp.CreateItem(olMailItem)

mymail.To = Cells(x, 2).Value
'.send


    With mymail
    .Subject = (xx)
    .Body =  (Message) (content of a cell) (message)...etc
    .Display
    End With

Cells(x, 10) = Date
Cells(x, 10).Interior.ColorIndex = 3
Cells(x, 10).Font.ColorIndex = 2
Cells(x, 10).Font.Bold = True

End If
End Function


Function comeon()
If daysLeft <= 7 And daysLeft >= 4 Then
Set myApp = New Outlook.Application
Set mymail = myApp.CreateItem(olMailItem)

mymail.To = Cells(x, 2).Value
'.send


    With mymail
    .Subject = (xx)
    .Body =  (Message) (content of a cell) (message)...etc
    .Display
    End With

Cells(x, 11) = Date
Cells(x, 11).Interior.ColorIndex = 3
Cells(x, 11).Font.ColorIndex = 2
Cells(x, 11).Font.Bold = True

End If
End Function
 Function late()
If daysLeft < 4 Then
Set myApp = New Outlook.Application
Set mymail = myApp.CreateItem(olMailItem)

mymail.To = Cells(x, 2).Value
'.send


    With mymail
    .Subject = (xx)
    .Body = (Message) (content of a cell) (message)...etc
    .Display
    End With

Cells(x, 12) = Date
Cells(x, 12).Interior.ColorIndex = 3
Cells(x, 12).Font.ColorIndex = 2
Cells(x, 12).Font.Bold = True

End If
End Function

engineer = Cell(x, 6).Value
If engineer = "PLM" Then
// here i should write the code that sends each email(functions created above to the engineer) 

Next

Set myApp = Nothing
Set mymail = Nothing

End Function

Thank you !! 谢谢 !! One last question: How can i show the info in a cell in between text in the .Body function?? 最后一个问题:如何在.Body函数中的文本之间的单元格中显示信息? This is what my excel sheet looks like The email has to be sent only when status is design, and the text of the email roughly look like this Dear (F2), This is a reminder that your dcp (A2) (b2) is due on the (G2), your Dcp (a3) (b3) is due on the (g3) error 13 screenshot 这就是我的excel工作表的样子电子邮件仅在状态为design时才必须发送,并且电子邮件的文本大致如下所示:亲爱的(F2),这提醒您dcp(A2)(b2)到期在(G2)上,您的Dcp(a3)(b3)是由于(g3) 错误13屏幕截图

This will be a general approach since we do not have your actual data. 由于我们没有您的实际数据,因此这将是一种通用方法。

As far as I understand you are creating a loop on the cases data as a start. 据我了解,您正在以案例数据为起点创建循环。 This is not a good way IMHO; 恕我直言,这不是一个好方法; if you set your first loop within e-mails data, then set a second loop within the cases data it will be much easier to handle the case. 如果您在电子邮件数据中设置了第一个循环,然后在案例数据中设置了第二个循环,则处理案例将更加容易。 The second loop adds each case to a string which will be used as the mail body afterwards. 第二个循环将每种情况添加到字符串中,然后将其用作邮件正文。 The condition is whether the e-mail of the case is equal to the one you are looping outside. 条件是该案例的电子邮件是否等于您要传递给外部的电子邮件。

After constructing the body for one e-mail (and if the body is not null), you will call the e-mail sending procedure. 在构造了一封电子邮件的正文之后(如果正文不为空),您将调用电子邮件发送过程。

I hope this helps, if not try providing some sample from your data which I or someone might create a functional code after then. 希望这对您有所帮助,如果不尝试从您的数据中提供一些示例,则此后我或某人可能会创建一个功能代码。

EDIT: Since you do not have a seperate e-mail addresses list, you shoul first create an array of the e-mails and then use that list as the outer loop. 编辑:由于您没有单独的电子邮件地址列表,因此您应该首先创建电子邮件的数组,然后将该列表用作外部循环。 I don't have the chance of trying but below code should somehow help you to get a start on the loops, e-mail bodt construction etc: 我没有机会尝试,但是下面的代码应该可以以某种方式帮助您开始循环,电子邮件Bodt构建等工作:

 Sub datesexcelvba()
 ' create a dictionary object of unique e-mails
     Dim d As Object, c As Range, k, tmp As String

    Set d = CreateObject("scripting.dictionary")
    For Each c In Range("H:H").Cells
        tmp = Trim(c.Value)
        If Len(tmp) > 0 Then d(tmp) = d(tmp) + 1
    Next c

    For Each k In d.keys
     ' This is the outer loop of e-mails, the body shoul be constructed here and the e-mail should be sent at the end.
     ' I am keeping your inner loop since I assume that there is no problem with it
        lastrow = Sheets("Messages english").Cells(Rows.Count, 1).End(xlUp).Row
            For x = 2 To lastrow

                If Cells(x, 4).Value = "Design" And Cells(x, 8).Value = k Then
                    myMail.Body = "Dcp No:" & Cells(x, 1).Value
                    myMail.Body = myMail.Body & " | Desc:" & Cells(x, 2).Value
                    myMail.Body = myMail.Body & " | Due Date:" & Cells(x, 7).Value
                    myMail.Body = myMail.Body & Chr(13) 'line feed
                 End If



            Next x
         If myEmail.Body <> "" Then Send_Mail k, "Task is due!", myMail.Body
    Next k


 End Sub

Function Send_Mail(ByVal email_recipient As String, ByVal email_subject, ByVal email_body As String)

    Dim myApp As Outlook.Application
    Set myApp = New Outlook.Application

    Dim myMail As Outlook.MailItem
    Set myMail = myApp.createItem(olMailItem)

    With myMail
        .To = email_recipient
        .Subject = email_subject
        .Body = email_body
        '.Display
    End With

    Set myMail = Nothing
    Set myApp = Nothing

End Function

Okay so here's a pieced together version of a solution that might work for you. 好的,这是一个可能适合您的解决方案的拼凑版本。 I noticed that you were missing the idea of a loop so I hope that you can at least work with this to make it do what you're looking for! 我注意到您错过了循环的想法,因此希望您至少可以使用它来使它完成您想要的工作!

Sub DCP_Emails()

    Dim mydate1 As Date
    Dim mydate2 As Long
    Dim datetoday1 As Date
    Dim datetoday2 As Long
    Dim daysLeft As Integer

    Dim lastRow As Integer
    lastRow = Sheets("Messages english").Cells(Rows.Count, 1).End(xlUp).Row

    Dim x As Integer
    For x = 2 To lastRow

        mydate1 = Cells(x, "C").value
        mydate2 = mydate1
        Cells(x, "G") = mydate2

        datetoday1 = Date
        datetoday2 = datetoday1


        Cells(x, "I").value = datetoday2
        daysLeft = mydate2 - datetoday2

        If LCase$(Cells(x, "D").Value2) = "design" Then
            If daysLeft <= 14 And daysLeft >= 8 Then
                Send_Mail Cells(x, "H").Value2, "DCP Reminder - Priority: Low", _
                        "Dear " & Cells(x, "F") & ", " & Chr(10) & Chr(10) & _
                        "This is a reminder that your DCP " & Cells(x, "A") & " " & Cells(x, "B") & " is due on " & Cells(x, "G")

            ElseIf daysLeft <= 7 And daysLeft >= 4 Then
                Send_Mail Cells(x, "H").Value2, "DCP Reminder - Priority: Medium", _
                        "Dear " & Cells(x, "F") & ", " & Chr(10) & Chr(10) & _
                        "This is a reminder that your DCP " & Cells(x, "A") & " " & Cells(x, "B") & " is due on " & Cells(x, "G")

            Else
                Send_Mail Cells(x, "H").Value2, "DCP Reminder - Priority: High", _
                        "Dear " & Cells(x, "F") & ", " & Chr(10) & Chr(10) & _
                        "This is a reminder that your DCP " & Cells(x, "A") & " " & Cells(x, "B") & " is due on " & Cells(x, "G")

            End If

            With Cells(x, "J")
                .Value2 = Date
                .Interior.ColorIndex = 3
                .Font.ColorIndex = 2
                .Font.Bold = True
            End With
        End If

    Next x

    Set myApp = Nothing

End Sub

Function Send_Mail(ByVal email_recipient As String, ByVal email_subject, ByVal email_body As String)

    Dim myApp As Outlook.Application
    Set myApp = New Outlook.Application

    Dim myMail As Outlook.MailItem
    Set myMail = myApp.createItem(olMailItem)

    With myMail
        .To = email_recipient
        .Subject = email_subject
        .Body = email_body
        '.Display
    End With

    Set myMail = Nothing
    Set myApp = Nothing

End Function

@hakan @hakan

    Sub DCP_Emails()

Dim mydate1 As Date
Dim mydate2 As Long
Dim datetoday1 As Date
Dim datetoday2 As Long
Dim daysLeft As Long
 Dim lastRow As Long


Dim d As Object, c As Range, k, tmp As String

Set d = CreateObject("scripting.dictionary")
 For Each c In Range("H:H").Cells
 If c.Value <> "N/A" Then
    tmp = Trim(c.Value)
    If Len(tmp) > 0 Then d(tmp) = d(tmp) + 1
  Next c




 For Each k In d.keys
lastRow = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row

Dim x As Long
For x = 2 To lastRow
    If Cells(x, 7).Value <> " " Then
        mydate1 = Cells(x, 7).Value
        mydate2 = mydate1
        Cells(x, "J") = mydate2

        datetoday1 = Date
        datetoday2 = datetoday1


        Cells(x, "K").Value = datetoday2
        daysLeft = mydate2 - datetoday2






        If LCase$(Cells(x, "D").Value2) = "design" And Cells(x, 8).Value = k Then

            If daysLeft <= 14 And daysLeft >= 8 Then

          Send_Mail k.Value2, "DCP Reminder - Priority: Low", _
         "Dear " & Cells(x, "F") & ", " & Chr(10) & Chr(10) & _
          "This is a reminder that your DCP " & Cells(x, "A") & " " & Cells(x, "B") & " is due on " & Cells(x, "G")

            End If


            With Cells(x, "L")
                .Value2 = Date
                .Interior.ColorIndex = 3
                .Font.ColorIndex = 2
                .Font.Bold = True
            End With
        End If
End If

Next x
Next k


Set myApp = Nothing

End Sub

Function Send_Mail(ByVal email_recipient As String, ByVal email_subject, ByVal email_body As String)

Dim myApp As Object
Set myApp = CreateObject("Outlook.Application")

Dim myMail As Object
Set myMail = myApp.createItem(0)

With myMail
    .To = email_recipient
    .Subject = email_subject
    .Body = email_body
    .Send
    '.Display
End With

Set myMail = Nothing
Set myApp = Nothing

End Function

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA如何在电子邮件正文更改时包含单元格内容 - Excel VBA how to include cell content on change in email body 当 ID 存储在一个 excel 单元格中时,如何发送多个 email? - How to send multiple email when ids are store in one excel cell? 如何将一封电子邮件发送到 Excel 工作簿中的电子邮件列表? excel VBA - How to send one email to list of emails in Excel workbook? Excel VBA 如何在 Outlook 中显示/发送带有可变 html 邮件正文内容的电子邮件(基于可变 Excel 单元格更改 - 基于列范围) - How to Display/Send an email in Outlook (based on variable Excel cell change - based on a column range) with variable html mail body content Excel在同一列中向所有人发送电子邮件,只需要向特定单元格中的所有人发送电子邮件。 怎么样? - Excel sending emails to all people in one Column, need to email only person in specific cell. How? Excel:根据单元格内容发送Outlook电子邮件 - Excel: Send outlook email based on cell content 如何在电子邮件正文中发送工作表? - How to send worksheet in email body? 从excel发送电子邮件。 放置正文单元格内容后,excel返回“#value!” 错误 - Send email from excel. After placing body cell content, excel returns “#value!” error 如何通过Excel在电子邮件的正文中发送多个范围? - How to send multiple ranges in the body of an email through Excel? 如何从Excel的电子邮件正文中发送形状对象? - How to send a shape object from Excel in the body of an Email?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM