将更改的行号添加到邮件正文

时间:2016-09-16 07:34:56

标签: excel vba excel-vba outlook

我在后台运行Outlook。使用Excel 2010。

我想做以下事情:

  • 向定义的人发送自动邮件
  • 邮件正文应包含发生更改的行号和列号
  • 它只适用于一个指定列中的单元格

我有以下内容:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim APP_OUTLOOK As Object
Dim MESSAGE As Object

Set APP_OUTLOOK = CreateObject("Outlook.Application")
Set MESSAGE = APP_OUTLOOK.CreateItem(0)

If Not Intersect(Target, Range("A1:A1048576")) Is Nothing Then
    With MESSAGE
        .To = "name.name@name.com"
        .Subject = ""
        .Body = "hey buddy, something has changed please have a look at cell: "
'the text should show: "hey buddy, something has changed please have a look at cell: A25000."
        .Display
    End With
End If
End Sub
  • 如果单元格中的值从""更改,它应该只发送邮件某事物或从价值变为另一个价值
  • 必须是动态的,因为我无法指定精确的单元格
  • 单元格包含格式为" dd.mm.yy"
  • 的日期

在扎克的帮助下,我能够解决我的问题。

以下是工作代码:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim APP_OUTLOOK As Object
Dim MESSAGE As Object

Set APP_OUTLOOK = CreateObject("Outlook.Application")
Set MESSAGE = APP_OUTLOOK.CreateItem(0)

    If Not Intersect(Target, Range("A1:A1048576")) Is Nothing Then
        With MESSAGE
            .To = "name.name@name.com"
            .Subject = ""
            .Body = "hey buddy," & vbNewLine & vbNewLine & "the file has been modified" & vbNewLine & vbNewLine & "following cell has a new entry: " & Target.Address & vbNewLine & vbNewLine & "the new entry has the value " & Target.Text & vbNewLine & vbNewLine & "please check the excel file" & vbNewLine & vbNewLine & "best wishes," & vbNewLine & "me"
            .Display
        End With
    End If
End Sub
  • 我添加了Target.Text来显示值,Target.Address用于显示单元格的行和列。

1 个答案:

答案 0 :(得分:0)

我没有测试过您的代码,但它看起来应该可行。要更改单元格的地址,请在Target.Address对帐单的末尾添加.Body