保存工作簿后在单元格编辑时触发邮件

Trigger mail on cell edits after saving workbook

提问人:Ajay Rathod 提问时间:7/31/2023 最后编辑:JohnMAjay Rathod 更新时间:10/2/2023 访问量:29

问:

我在互联网上找到了代码,该代码在工作表级别触发了有关单元格编辑的电子邮件。

但是,我只想在进行更改后保存工作簿后才触发整个工作簿的电子邮件。

Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 2017/9/12
    Dim xRgSel As Range
    Dim xOutApp As Object
    Dim xMailItem As Object
    Dim xMailBody As String
    On Error Resume Next
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Set xRg = Range("A2:E11")
    Set xRgSel = Intersect(Target, xRg)
    ActiveWorkbook.Save
    If Not xRgSel Is Nothing Then
        Set xOutApp = CreateObject("Outlook.Application")
        Set xMailItem = xOutApp.CreateItem(0)
        xMailBody = "Cell(s) " & xRgSel.Address(False, False) & _
          " in the worksheet '" & Me.Name & "' were modified on " & _
          Format$(Now, "mm/dd/yyyy") & " at " & Format$(Now, "hh:mm:ss") & _
          " by " & Environ$("username") & "."

        With xMailItem
            .To = "Email Address"
            .Subject = "Worksheet modified in " & ThisWorkbook.FullName
            .Body = xMailBody
            .Attachments.Add (ThisWorkbook.FullName)
            .Display
        End With
        Set xRgSel = Nothing
        Set xOutApp = Nothing
        Set xMailItem = Nothing
    End If
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
VBA 电子邮件 事件 Outlook

评论

0赞 Tim Williams 7/31/2023
一种方法是(例如)在工作簿打开时保存工作簿的副本,然后在保存时比较两个工作簿之间的内容以查找更改。如果您只是逐个单元格记录更改,则需要考虑用户、恢复编辑等。

答:

0赞 Eugene Astafiev 7/31/2023 #1

更改工作表时,您可以设置一个布尔标志,该标志可用于决定是否在工作簿关闭时发送电子邮件。

Dim flag as Boolean = False

Private Sub Worksheet_Change(ByVal Target As Range)
  flag = True
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel as Boolean) 
    If flag = False Then 
      SendEmail()
    End If 
End Sub

Private Sub SendEmail()
   Dim xRgSel As Range
    Dim xOutApp As Object
    Dim xMailItem As Object
    Dim xMailBody As String
    On Error Resume Next
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Set xRg = Range("A2:E11")
    Set xRgSel = Intersect(Target, xRg)
    ActiveWorkbook.Save
    If Not xRgSel Is Nothing Then
        Set xOutApp = CreateObject("Outlook.Application")
        Set xMailItem = xOutApp.CreateItem(0)
        xMailBody = "Cell(s) " & xRgSel.Address(False, False) & _
            " in the worksheet '" & Me.Name & "' were modified on " & _
            Format$(Now, "mm/dd/yyyy") & " at " & Format$(Now, "hh:mm:ss") & _
            " by " & Environ$("username") & "."

        With xMailItem
            .To = "Email Address"
            .Subject = "Worksheet modified in " & ThisWorkbook.FullName
            .Body = xMailBody
            .Attachments.Add (ThisWorkbook.FullName)
            .Display
        End With
        Set xRgSel = Nothing
        Set xOutApp = Nothing
        Set xMailItem = Nothing
    End If
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

当触发 Workbook.BeforeSave 事件时(在保存工作簿之前),您可以触发发送电子邮件以发送出去。