Friday, November 6, 2009

How would you write a macro to send an email from excel?

I use excel as a database with information. It stores names of multiple people so I would want an email from outlook to go out to 3 addresses at once (all in the To: field). I have about 300 emails to send but the text body is the same for everyone.

How would you write a macro to send an email from excel?
Example code for sending mail via Outlook is below.





Sub SendMsg()


Dim oMessage As MailItem


Set oMessage = CreateItem(olMailItem)


oMessage.To = "someone@somewhere.com"


oMessage.CC = "someoneelse@somewhereelse.com"


oMessage.BCC = "anyone@anywhere.net; everyone@everywhere.net”


oMessage.Subject = "A Very Important or Unimportant Message"


oMessage.Body = "A very important message, trivia, or anything in between"


oMessage.Send


Set oMessage = Nothing


End Sub





Be sure to add Microsoft Outlook 11.0 Object Library to your References (Tools %26gt; References in the Microsoft Visual Basic window.)





You can change the values and repeat the Send as often as necessary.





If you are sending a message to a large number of people, you should consider using the BCC for all of their addresses and putting your email address in the To. Long address list attract spammers like a light attracts moths.





No comments:

Post a Comment

Blog Archive