简体   繁体   English

VBA根据工作表输入向员工发送电子邮件

[英]VBA Send Email to Employee based on Worksheet input

In my Worksheet, a Master-User is putting in Information for every Employee - more precisely whether the Employees handed in two forms in the end of the month. 在我的工作表中,主用户正在为每位员工输入信息-更确切地说,员工是否在月底提交了两种表格。

What I am trying to achieve is that a reminder-Email is send to everyone listed on Sheet1 who has the answer "No" in his row - so he/she did not hand in the form. 我要达到的目的是,将一封提醒电子邮件发送给Sheet1上列出的在其行中回答为“否”的所有人-因此他/她没有上交表格。

However, I do not know how to store the Information for all missing documents for one Employee before sending an Email to this Person. 但是, 在向该员工发送电子邮件之前 ,我不知道如何为一位员工存储所有丢失文件的信息 So in my current Loop, if an Employee forgot to Hand in 2-3 documents over the course of a few months, he/she would get 2-3 different Emails. 因此,在我当前的循环中,如果某个员工在几个月内忘记交出2-3个文档,他/她将收到2-3个不同的电子邮件。

So looking at the picture below, Maxime Musterman would get an Email saying: 因此,看下面的图片,马克西姆·马斯特曼会收到一封电子邮件,内容为:


"Hey Maxime, “嘿马克西姆,

I am still missing your: 我仍然想念你:

  • Stmnt of no Export from Aug 16 从8月16日开始禁止出口

  • Stmnt of no Export from Sep 16 从9月16日开始不出口

Thanks" 谢谢”


Maybe one of you could help? 也许你们其中之一可以帮助您?

I am new to VBA and got the Email-sending-Code from another Website. 我是VBA的新手,并且从另一个网站获得了电子邮件发送代码。

Thanks in advance! 提前致谢!

在此处输入图片说明

'----------------------------------------------------------------------------------------------------------------
'#################Set Email Conditions#################
'----------------------------------------------------------------------------------------------------------------

Dim CDO_Mail As Object
Dim CDO_Config As Object
Dim SMTP_Config As Variant
Dim strSubject As String
Dim strFrom As String
Dim strTo As String
Dim strCc As String
Dim strBcc As String
Dim strBody As String

Set CDO_Mail = CreateObject("CDO.Message")

Set CDO_Config = CreateObject("CDO.Configuration")
CDO_Config.Load -1

Set SMTP_Config = CDO_Config.Fields

With SMTP_Config
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.metrocast.net"
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    .Update
End With

'----------------------------------------------------------------------------------------------------------------
'#################Find who needs a Reminder-Email#################
'----------------------------------------------------------------------------------------------------------------

Dim ws As Worksheet, wsOutput As Worksheet
Set ws = ActiveWorkbook.Worksheets("Sheet1")
Set wsOutput = ActiveWorkbook.Worksheets("Sheet2")
Dim MonthYearInput As String, recipientName As String, recipientEmail As String
Dim Employee As Range, DocsInMonth As Range
Dim lRow As Long, lcol As Long, NextRow As Long

lRow = ws.Range("A" & Rows.Count).End(xlUp).Row
lcol = ws.Cells(11, Columns.Count).End(xlToLeft).Column


For Each Employee In ws.Range(Cells(12, 1), Cells(lRow, 1))
    For Each DocsInMonth In ws.Range(Cells(Employee.Row, 4), Cells(Employee.Row, lcol))

        If ws.Cells(Employee.Row, DocsInMonth.Column) = "No" And _
        DateValue(ws.Cells(10, DocsInMonth.Column)) >= DateValue(ws.Cells(Employee.Row, 3)) Then

            recipientName = ws.Cells(Employee.Row, 1)
            recipientEmail = ws.Cells(Employee.Row, 2)

            'How to store the information regarding all missing infos before sending it
            'to avoid sending 2-3 Emails to the same person?

            strSubject = "Results from Excel Spreadsheet"
            strFrom = "me@gmail.com"
            strTo = recipientEmail
            strCc = ""
            strBcc = ""
            strBody = "Hey " & recipientName & vbNewLine & vbNewLine & vbNewLine & _
                      "I am still missing INFO INFO INFO"

            With CDO_Mail
                Set .Configuration = CDO_Config
            End With

            CDO_Mail.Subject = strSubject
            CDO_Mail.From = strFrom
            CDO_Mail.To = strTo
            CDO_Mail.TextBody = strBody
            CDO_Mail.CC = strCc
            CDO_Mail.BCC = strBcc
            CDO_Mail.Send

        End If
    Next DocsInMonth
Next Employee

-------------------------------------------------------------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ----

EDIT: 编辑:

I tried another approach by copying all Entries with a "No" for an Employee onto the second Sheet and then attaching the Sheet2 to the Email. 我尝试了另一种方法,将所有雇员的所有带有“否”的条目复制到第二张工作表,然后将Sheet2附加到电子邮件。 However, I am getting a Runtime Error 13 "Type Mismatch" at this statement: 但是,我在此语句中收到运行时错误13“类型不匹配”:

If ws.Cells(Employee.Row, DocsInMonth.Column) = "No" And _
        DateValue(ws.Cells(10, DocsInMonth.Column)) >= DateValue(ws.Cells(Employee.Row, 3)) Then

Code: 码:

Dim ws As Worksheet, wsOutput As Worksheet
Set ws = ActiveWorkbook.Worksheets("Sheet1")
Set wsOutput = ActiveWorkbook.Worksheets("Sheet2")
Dim recipientName As String, recipientEmail As String
Dim Employee As Range, DocsInMonth As Range
Dim lRow As Long, lcol As Long, NextRow As Long

lRow = ws.Range("A" & Rows.Count).End(xlUp).Row
lcol = ws.Cells(11, Columns.Count).End(xlToLeft).Column


For Each Employee In ws.Range(Cells(12, 1), Cells(lRow, 1))
    For Each DocsInMonth In ws.Range(Cells(Employee.Row, 4), Cells(Employee.Row, lcol))
        If ws.Cells(Employee.Row, DocsInMonth.Column) = "No" And _
        DateValue(ws.Cells(10, DocsInMonth.Column)) >= DateValue(ws.Cells(Employee.Row, 3)) Then

            NextRow = wsOutput.Range("A" & Rows.Count).End(xlUp).Row + 1
            ws.Cells(Employee.Row, 1).Copy Destination:=wsOutput.Cells(NextRow, 1) 'Name
            ws.Cells(10, DocsInMonth.Column).Copy Destination:=wsOutput.Cells(NextRow, 2) 'Month
            ws.Cells(11, DocsInMonth.Column).Copy Destination:=wsOutput.Cells(NextRow, 3) 'What


        End If
    Next DocsInMonth
Next Employee

In your second attempt, there is a potential problem with using merged cells, and this line will likely throw an error when DocsInMonth.Column is an odd number column (such as Column E, G, etc.). 第二次尝试,使用合并单元格可能会出现问题,并且当DocsInMonth.Column是奇数列(例如Column E,G等)时,此行可能会引发错误。 This part of your If statement will raise an error: If语句的这一部分将引发错误:

DateValue(ws.Cells(10, DocsInMonth.Column))

The reason being, when DocsInMonth.Column = 5, for example, then ws.Cells(10,5) is part of a merged range, and actually the value in that cell E5 is empty, the value exists only in D5 . 原因是,例如,当DocsInMonth.Column = 5时, ws.Cells(10,5)是合并范​​围的一部分,而实际上该单元格E5中的值为空,则该值仅存在于D5中

This should resolve it, by forcing the code to look at the first cell in the MergeArea : 这应该通过强制代码查看MergeArea中的第一个单元格来MergeArea

DateValue(ws.Cells(10, DocsInMonth.Column).MergeArea.Cells(1).Value)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM