简体   繁体   中英

Excel VBA code to read a username from a cell then send an email to that user

I require some help in creating Excel VBA code which will read a row of usernames from cells in Excel and then send an email to all those users by searching for the users email address in the Outlook contacts list.

I have managed to write the code that will bring up outlook's compose email dialog box from the spreadsheet.

You can use for in range with mails and call this proc to send email

Public Sub SendMail(MailTO As String, MailSubject As String, MailBody As String)
'http://officevb.com

Dim appOL               As Object
Dim myEmail             As Object
Dim TxtHello            As String

Set appOL = CreateObject("Outlook.Application")
Set myEmail = appOL.CreateItem(olMailItem)

'Use hour to create a text
Select Case Hour(Time)
    Case Is <= 12
        TxtHello = "Good Morning," & vbNewLine
    Case Is >= 12
        TxtHello = "Good Afternoom," & vbNewLine
    Case Is >= 18
        TxtHello = "Good Night," & vbNewLine
End Select

    With myEmail
      .display
      .Recipients.Add MailTO
      .Subject = MailSubject
      .Body = TxtHello & MailBody
      .Send
    End With

Set myEmail = Nothing
Set appOL = Nothing

End Sub

call this sub passing these parameters

sendMail "Mail@yourContact.com","Test","This is a automatic mail"

[]´s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM