<< Click to Display Table of Contents >> Navigation: Samples and Tutorials > VBA (Microsoft Office) > Using AddEmail in VBA projects |
AddEmail ActiveX can be used to send emails from any Microsoft Office application that supports Visual Basic for Applications (VBA), such as Microsoft Access, Microsoft Excel, Microsoft Word etc. To use AddEmail in your VBA project perform the following steps:
1. (Optional) Add reference to AddEmail library: In VBA editor menu, select Tools -> References. Find and select AddEmail 4.0 Type Library.
2. Add a code that creates SmtpMail object and sends a message using SimpleSend, SimpleSendHtml, SimpleSendAttachment or Send. Please use code snapshots below to get started.
Snapshot 1: simple text email.
Dim objSmtpMail As Object, strError As String, numResultCode As Long
Set objSmtpMail = CreateObject("AddEmail.SmtpMail")
objSmtpMail.SmtpServer = "mail.myserver.com"
objSmtpMail.SmtpUsername = "jsmith"
objSmtpMail.SmtpPassword = "mypassword"
numResultCode = objSmtpMail.SimpleSend("jsmith@myserver.com", "jane@someserver.com;james@someserver.com", "test", "Test message", strError)
If numResultCode = 0 Then
MsgBox "Sent successfully!"
Else
MsgBox strError
End If
Snapshot 2: text email with attachments.
Dim objSmtpMail As Object, strError As String, numResultCode As Long
Set objSmtpMail = CreateObject("AddEmail.SmtpMail")
objSmtpMail.SmtpServer = "mail.myserver.com"
objSmtpMail.SmtpUsername = "jsmith"
objSmtpMail.SmtpPassword = "mypassword"
numResultCode = objSmtpMail.SimpleSendAttachment("jsmith@myserver.com", "jane@someserver.com;james@someserver.com", "test", "Test message", "c:\files\doc1.pdf;c:\files\doc2.pdf", strError)
If numResultCode = 0 Then
MsgBox "Sent successfully!"
Else
MsgBox strError
End If
Snapshot 3: HTML message with attachments.
Dim objSmtpMail As Object, strError As String, numResultCode As Long
Set objSmtpMail = CreateObject("AddEmail.SmtpMail")
objSmtpMail.SmtpServer = "mail.myserver.com"
objSmtpMail.SmtpUsername = "jsmith"
objSmtpMail.SmtpPassword = "mypassword"
' Create message and setup subject and body
Dim objMailMessage As Object
Set objMailMessage = CreateObject("AddEmail.MailMessage")
objMailMessage.MessageBodyFormat = 1 'HTML format
objMailMessage.MessageSubject = "test"
objMailMessage.MessageBody = "<html><body><b>Testing...</b></body></html>"
' Add first attachment
Dim objMailAttachment As Object
Set objMailAttachment = CreateObject("AddEmail.MailAttachment")
objMailAttachment.File = "c:\files\doc1.pdf"
objMailMessage.AddAttachment objMailAttachment
' Add second attachment
Set objMailAttachment = CreateObject("AddEmail.MailAttachment")
objMailAttachment.File = "c:\files\doc2.pdf"
objMailMessage.AddAttachment objMailAttachment
' Setup sender
Dim objMailAddress As Object
Set objMailAddress = CreateObject("AddEmail.MailAddress")
objMailAddress.Name = "John Smith"
objMailAddress.Address = "jsmith@myserver.com"
objMailMessage.Sender = objMailAddress
' Setup first recipient
Set objMailAddress = CreateObject("AddEmail.MailAddress")
objMailAddress.Name = "Jane Smith"
objMailAddress.Address = "jane@someserver.com"
objMailMessage.AddRecipient objMailAddress
' Setup second recipient
Set objMailAddress = CreateObject("AddEmail.MailAddress")
objMailAddress.Name = "James Smith"
objMailAddress.Address = "james@someserver.com"
objMailMessage.AddRecipient objMailAddress
' Send prepared message
numResultCode = objSmtpMail.Send(objMailMessage, True, strError)
If numResultCode = 0 Then
MsgBox "Sent successfully!"
Else
MsgBox strError
End If
Please refer to the Reference section of this manual for detailed description of AddEmail objects, methods and properties.