[英]Excel: Send outlook email based on cell content
I would like to learn: 我乐意去学:
When it is 16 days past the date in a certain column(column D), how do we send an outlook email to the email address (format: name@abc.com) which is listed in another column C. eg. 如果某个日期(D列)中的日期比日期晚16天,我们如何将Outlook电子邮件发送到另一列C中列出的电子邮件地址(格式:name@abc.com)。 An email should be sent to the email address in C1 if the date is 16 days past the date in cell D1 and the cell E1 is blank(has no text).
如果日期晚于单元格D1中的日期16天并且单元格E1为空白(无文本),则应将电子邮件发送到C1中的电子邮件地址。
Also, how can I set up the body of the email in a way such that I can insert a string into the body by taking the text in another column B. eg. 另外,如何设置电子邮件的正文,以便可以通过将文本插入另一列B中来将字符串插入正文中。 the body of the email sent to the email address in cell C1 should contain the string of text in cell B1.
发送到单元格C1中电子邮件地址的电子邮件正文应在单元格B1中包含文本字符串。
Please add a Worksheet_Change
event on the code page of your worksheet where you are having data as shown in your snapshot.Open VB Editor by pressing F11
key. 请在
Worksheet_Change
的代码页上添加一个Worksheet_Change
_ Worksheet_Change
事件,其中包含快照中显示的数据。按F11
键打开VB编辑器。 Then you click on sheet, it will open up code page of the sheet. 然后单击工作表,它将打开工作表的代码页。 From the drop down select change and a routine will be added to the code page.
从下拉列表中选择更改,然后将一个例程添加到代码页。 Insert code in that routine.
在该例程中插入代码。
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 7 Then 'e.g. for column G
Sendmail 'name of your sub
End If
End Sub
Set up of our sheet is like the snap shot shown below. 我们的工作表设置如下图所示。
I have considered following points.
我考虑了以下几点。
F2
cell which will be "YES"
or "NO"
depending upon date in D2
cell and E2
status. F2
单元格中输入了以下公式,根据D2
单元格中的日期和E2
状态,该公式将为"YES"
或"NO"
。 Column G
has to be clicked. Column G
的单元格。 I have added a form control button in cell G2
. G2
添加了一个表单控制按钮。 When status is yes click the button in G2
to send the mail. G2
的按钮发送邮件。 Sendmail
in that module. Sendmail
的代码放入该模块中。 Subject is in cell F1
, body of message apart from general salutations is in cell B1
, Name of the recipient is in cell C2
主题位于单元格
F1
,除了一般称呼之外,消息正文位于单元格B1
,收件人的名称位于单元格C2
Sub Sendmail() ' Display a message when one of the designated cells has been ' changed. ' Place your code here. Dim answer As String Dim SubmitLink As String Dim KeyCells As Range Set KeyCells = Range("F2:F100") SubmitLink = Range("B1").Value answer = MsgBox("Do you wish to save this change. An Email will be sent to the User", vbYesNo, "Save the change") If answer = vbNo Then Cancel = True If answer = vbYes Then Application.EnableEvents = False Application.ScreenUpdating = False 'open outlook type stuff Set OutlookApp = CreateObject("Outlook.Application") Set OlObjects = OutlookApp.GetNamespace("MAPI") Set newmsg = OutlookApp.CreateItem(olMailItem) On Error Resume Next 'add recipients 'newmsg.Recipients.Add ("Name Here") newmsg.Recipients.Add Worksheets("Sheet1").Range("C2").Value 'add subject newmsg.Subject = Worksheets("Sheet1").Range("F1").Value 'add body newmsg.Body = "Dear Customer, " & SubmitLink & vbLf & vbLf & vbLf & " Look Forward to your confirmation" & vbLf & vbLf & vbLf & "Sincerely," & vbLf & "Customer Care department" newmsg.Display 'display newmsg.Send 'send message 'give conformation of sent message MsgBox "Modification confirmed", , "Confirmation" End If ' MsgBox "Cell " & Target.Address & " has changed." On Error GoTo 0 Set newmsg = Nothing Set OutlookApp =Nothing End Sub
EDIT : OP comments location of codes shown in snap shots 编辑:OP注释快照中显示的代码位置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.