使用Excel电子表格中的数据填充电子邮件

时间:2016-11-10 15:08:15

标签: excel vba email

我有一个联系人的Excel电子表格。我想设置一个下拉列表,向我选择的特定人员发送电子邮件,并在电子邮件正文中返回联系信息。

我不知道如何将电子邮件自动填充到现在,弹出的电子邮件已经" true"在联系人信息的正文中,而不是在单元格中返回文本值。

Sub DropDown7_Change()

    Dim answer As String

    answer = MsgBox("Are you sure you want to assign this lead?", _
        vbYesNo, "Send Email")
    '    Above code informs the user that an automated email will be sent

    'Code uses the users answer to either carryout the generated email process or to not save the changes.
    If answer = vbNo Then Cancel = True
    If Cancel = True Then Exit Sub

    If answer = vbYes Then  
        'Connects to outlook and retrieves information needed to create and send the email.
        Set OutlookApp = CreateObject("Outlook.Application")
        Set OlObjects = OutlookApp.GetNamespace("MAPI")
        Set newmsg = OutlookApp.CreateItem(olMailItem)

        'Contains the email address of the person receiving the email.
        newmsg.Subject = "Lead Assigned to You" 'Sets the automated subject line to the email
        newmsg.Body = "Hello," & vbNewLine & _
            "You have been assigned a lead. Please follow up with the contact" & vbNewLine & _
            ActiveCell.Offset(0, 3).Range("K5").Select
            ActiveCell.Offset(0, 6).Range("K5").Select
            ActiveCell.Offset(0, 7).Range("K5").Select

        'Above code has the body of the automated email
        newmsg.Display
    End If

End Sub ' End of function

1 个答案:

答案 0 :(得分:0)

如果您尝试将Offset的值设为Range("K5"),则需要将Offset.Value一起使用,例如Range("K5").Offset(0, 3).Value ,这将获得Cell" K5"右侧3列的值。

下面的代码,将3个单元格中的值与列偏移量添加到单元格" K5"给你发邮件正文:

Sub DropDown7_Change()

    Dim answer As String

    answer = MsgBox("Are you sure you want to assign this lead?", _
        vbYesNo, "Send Email")
    '    Above code informs the user that an automated email will be sent

    'Code uses the users answer to either carryout the generated email process or to not save the changes.
    If answer = vbNo Then
        Exit Sub
    Else
        If answer = vbYes Then

            'Connects to outlook and retrieves information needed to create and send the email.
            Set OutlookApp = CreateObject("Outlook.Application")
            Set OlObjects = OutlookApp.GetNamespace("MAPI")
            Set newmsg = OutlookApp.CreateItem(olMailItem)

            'Contains the email address of the person receiving the email.
            newmsg.Subject = "Lead Assigned to You" 'Sets the automated subject line to the email
            newmsg.body = "Hello," & vbNewLine & _
                "You have been assigned a lead. Please follow up with the contact" & vbNewLine & _
                Range("K5").Offset(0, 3).Value & vbNewLine & _
                Range("K5").Offset(0, 6).Value & vbNewLine & _
                Range("K5").Offset(0, 7).Value & vbNewLine                    

            'Above code has the body of the automated email
            newmsg.Display
        End If
    End If

End Sub