【问题标题】:Send a second E-Mail automatically once the first E-Mail is sent (Excel-VBA-Code)发送第一封电子邮件后自动发送第二封电子邮件(Excel-VBA-代码)
【发布时间】:2017-06-30 22:50:51
【问题描述】:

我有以下 VBA 代码来发送电子邮件:

Sub First_Email()
    If ExitAll = False Then
        Dim OApp As Object, OMail As Object, signature As String
        Set OApp = CreateObject("Outlook.Application")
        Set OMail = OApp.CreateItem(0)
            With OMail
            .Display
            End With
            signature = OMail.HTMLbody
            With OMail
            .To = "test@test.de"
            .Subject = "First E-Mail"
            .HTMLbody = "<p> Once I click on the send button for this E-Mail I want that the E-Mail in the code below is sent as well.</p>"
            End With
        Set OMail = Nothing
        Set OApp = Nothing
    Else
    End If
End Sub

此代码使用上述电子邮件打开 Outlook。由于用户在发送电子邮件之前仍有机会修改电子邮件,因此代码不会自动发送电子邮件。

一旦用户点击发送按钮,电子邮件就会被发送。现在,我想实现一旦用户单击第一封电子邮件的发送按钮,下面的电子邮件也会自动发送:

Sub Second_Email()
    If ExitAll = False Then
        Dim OApp As Object, OMail As Object, signature As String
        Set OApp = CreateObject("Outlook.Application")
        Set OMail = OApp.CreateItem(0)
            With OMail
            .Display
            End With
            signature = OMail.HTMLbody
            With OMail
            .To = "test@test.de"
            .Subject = "Second E-Mail"
            .HTMLbody = "<p> Once I click on the send button for the first E-Mail I want that this E-Mail is sent as well.</p>"
            .send
            End With
        Set OMail = Nothing
        Set OApp = Nothing
    Else
    End If
End Sub

与第一封电子邮件相比,用户没有机会在第二封电子邮件发送之前对其进行修改。一旦用户单击第一封电子邮件的发送按钮,它应该立即发送。

您知道如何实现在用户点击第一封电子邮件中的发送按钮后自动发送第二封电子邮件吗?

【问题讨论】:

    标签: excel email outlook vba


    【解决方案1】:

    您可以在 Outlook 中使用 ItemSend。在 ThisOutlookSession 模块中。

    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    
        If Item.To = "test@test.de" Then
    
            If Item.Subject = "First E-Mail" Then
    
                Set OMail = CreateItem(0)
    
                With OMail
                    .To = "test@test.de"
                    .Subject = "Second E-Mail"
                    .HTMLbody = "<p> Once I click on the send button for the first E-Mail I want that this E-Mail is sent as well.</p>"
                    .send
                End With
    
            End If
        end if
    
    End Sub
    

    注意:这将在所有邮件上运行,而不仅仅是 Excel 中 First_Email 发起的邮件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      • 1970-01-01
      • 2011-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多