Excel VBA如何在电子邮件正文中包含更改单元格内容

时间:2016-02-09 05:23:50

标签: excel vba excel-vba email outlook-vba

你好的编码员和知识寻求者。我有这个代码发送一封电子邮件,通知日期已添加到J列(提交日期)中的单元格。我只想在B列(提交标题)中包含与已添加日期相对应的单元格内容。

当我在第J列的单元格中添加日期时,代码工作正常并发送电子邮件。但我想在电子邮件正文中添加该提交标题。这是我的代码

BLOB

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

这是解决方案。只需定义要从

中检索数据的单元格
Dim SubmitLink As String

然后从KeyCell

中识别此单元格的偏移量
SubmitLink = Target.Offset(, -8).Value

最后在电子邮件正文或标题的文本中添加(“& SubmitLink&”),并显示该单元格中的数据。这是完整的代码

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim KeyCells As Range


    ' The variable KeyCells contains the cells that will
    ' cause an alert when they are changed.
    Set KeyCells = Range("J3:J1000")


    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then

        ' Display a message when one of the designated cells has been
        ' changed.
        ' Place your code here.
Dim answer As String
Dim SubmitLink As String

SubmitLink = Target.Offset(, -8).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
'open outlook type stuff
Set OutlookApp = CreateObject("Outlook.Application")
Set OlObjects = OutlookApp.GetNamespace("MAPI")
Set newmsg = OutlookApp.CreateItem(olMailItem)
'add recipients
'newmsg.Recipients.Add ("Name Here")
newmsg.Recipients.Add Worksheets("Coordinator").Range("Q4").Value
'add subject
newmsg.Subject = Worksheets("Coordinator").Range("O3").Value
'add body
newmsg.Body = "Dear User, New Submittal ( " & SubmitLink & " ) has been Added in Submittal Log. Please Investigate the Change" & vbLf & vbLf & vbLf & "Sincerely," & vbLf & "logs 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."

End If
End Sub