根据单元名称发送电子邮件

时间:2019-01-19 06:49:18

标签: excel vba outlook

我想在G列中添加特定单元格名称时发送自动电子邮件,例如,当您在G列单元格的任何单元格中键入(订单)的名称时,它会自动从Outlook发送电子邮件。

这是使用的代码

Private Sub CommandButton7_Click()
    Dim Email_Subject, Email_Send_From, Email_Send_To, _
    Email_Cc, Email_Bcc, Email_Body As String
    Dim Mail_Object, Mail_Single As Variant

    Email_Subject = " "
    Email_Send_From = ""
    Email_Send_To = ""
    Email_Cc = ""
    Email_Bcc = ""
    Email_Body = ""

    On Error GoTo debugs
    Set Mail_Object = CreateObject("Outlook.Application")
    Set Mail_Single = Mail_Object.CreateItem(0)
    With Mail_Single
        .Subject = Email_Subject
        .To = Email_Send_To
        .CC = Email_Cc
        .BCC = Email_Bcc
        .Body = Email_Body
        .send
    End With

   debugs:
   If Err.Description <> "" Then MsgBox Err.Description
End Sub

2 个答案:

答案 0 :(得分:0)

假设您现有的代码已经很好,那么您应该:

  • 在IDE的左上角转到“项目树”
  • 点击您要监控的工作表

...并在其中输入原始代码(已修复(以便对其进行参数处理,而不是使用电子邮件地址,主题等的硬编码值)):

Private Sub sendEmail(ByVal Email_Subject As String, ByVal Email_Send_From As String, _
                      ByVal Email_Send_To As String, ByVal Email_Cc As String, _
                      ByVal Email_Body As String)

    On Error GoTo debugs
    Set Mail_Object = CreateObject("Outlook.Application")
    Set Mail_Single = Mail_Object.CreateItem(0)
    With Mail_Single
        .Subject = Email_Subject
        .To = Email_Send_To
        .CC = Email_Cc
        .BCC = Email_Bcc
        .Body = Email_Body
        .send
    End With

debugs:
   If Err.Description <> "" Then MsgBox Err.Description

End Sub

...和事件Worksheet_Change的侦听器,每次修改的单元格在列G中时,都会调用Sub sendEmail(在此示例中,我正在传递在参数Offset(0(同一行), -n)中(其中nG之前或之后的几列,例如Email_address之类的信息):

Private Sub Worksheet_Change(ByVal Target As Range)
    If Split(Target.Address, "$")(1) = "G" And Target.Value = "magic words" Then
        sendEmail Target.Offset(0, -1), Target.Offset(0, -2), ...
    End If
End Sub

仅供参考,例如,当我说Target.Offset(0,-2)时,如果TargetRange("G4")(假设)是Target.Offset(0,-2),则Range("E4")将是{{1} }(同一行0,在-2前两列)。

答案 1 :(得分:0)

您的变量未完全标记,每个字符串都需要变暗为字符串。

在此示例中,电子邮件地址在E列中。好主意@Matteo NNZ?

只需将几行af添加到代码顶部并放置在工作表change事件中即可。

将代码复制并粘贴到工作表模块中。

enter image description here

更改G列中的单元格,代码将产生电子邮件并使用E列中的电子邮件地址。

- enter image description here

完整代码:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Target.Column <> 7 Then Exit Sub

    Dim Email_Subject As String
    Dim Email_Send_To As String, Email_Cc, Email_Bcc As String
    Dim mail_Body As String, Mail_Object, Mail_Single As Variant

    Email_Subject = Target
    Email_Send_To = Target.Offset(, -2)
    Email_Cc = ""
    Email_Bcc = ""
    Email_Body = ""

    On Error GoTo debugs
    Set Mail_Object = CreateObject("Outlook.Application")
    Set Mail_Single = Mail_Object.CreateItem(0)
    With Mail_Single
        .Subject = Email_Subject
        .To = Email_Send_To
        .CC = Email_Cc
        .BCC = Email_Bcc
        .Body = Email_Body
        '.send
        .display
    End With

debugs:
    If Err.Description <> "" Then MsgBox Err.Description
End Sub