excel: reference cell value to get email recipient for selected row?

﹥>﹥吖頭↗ 提交于 2019-12-12 02:06:14

问题


I am trying to populate my email to field with the email address stored as a value in cells AF.

AF8 = info@email.com
AF9 = help@email.com
AF10 = hello@email.com

I am trying to use select range as each AF cell in every row contains a different email address, so when a user has a row selected is should get the appropriate cell value from AF for that row.

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Column = Range("CA8").Column Then

        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 = "New Supplier Set-Up Confirmation"
        Email_Send_From = "purchasing@hewden.co.uk"
        Email_Send_To = Range("AF8").Select
        Email_Cc = "databison@gmail.com"
        Email_Bcc = "databison@gmail.com"
        Email_Body = "Congratulations!!!! You have successfully sent an e-mail using VBA !!!!"

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

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

    End If
End Sub

However when I try this code it comes up with the error, "outlook does not recognise one or more names"


回答1:


.To expects a string

http://msdn.microsoft.com/en-us/library/office/ff860378%28v=office.15%29.aspx

"...sets a semicolon-delimited String list..."

Email_Send_To = Range("AF8").Value

Once Email_Send_To is a string value then

.To = Email_Send_To


来源:https://stackoverflow.com/questions/25242301/excel-reference-cell-value-to-get-email-recipient-for-selected-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!