[英]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: Send outlook email based on cell content
请在Worksheet_Change
的代码页上添加一个Worksheet_Change
_ Worksheet_Change
事件,其中包含快照中显示的数据。按F11
键打开VB编辑器。 然后单击工作表,它将打开工作表的代码页。 从下拉列表中选择更改,然后将一个例程添加到代码页。 在该例程中插入代码。
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
我们的工作表设置如下图所示。
F2
单元格中输入了以下公式,根据D2
单元格中的日期和E2
状态,该公式将为"YES"
或"NO"
。 Column G
的单元格。 我在单元格G2
添加了一个表单控制按钮。 当状态为是时,单击G2
的按钮发送邮件。 Sendmail
的代码放入该模块中。 主题位于单元格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
编辑:OP注释快照中显示的代码位置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.