繁体   English   中英

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

[英]Excel VBA how to include cell content on change in email body

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

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

  Private Sub Worksheet_Change(ByVal Target As Range)

    Dim KeyCells As Range
    Dim SubmitLink As Range

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

    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

        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" & Cells(SubmitLink.Row, "B") & "has been Added in SUBMITTAL Log. Please Investigate the Change"
            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

感谢您的帮助

这就是解决方案。 只需定义要从中检索数据的单元格

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

暂无
暂无

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

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