Sending outlook emails from Excel - excel

I have the below VBA that sends an email fine:
With Sendrng
' Select the worksheet with the range you want to send
.Parent.Select
'Remember the ActiveCell on that worksheet
Set rng = ActiveCell
'Select the range you want to mail
.Select
' Create the mail and send it
ActiveWorkbook.EnvelopeVisible = True
With .Parent.MailEnvelope
' Set the optional introduction field thats adds
' some header text to the email body.
.Introduction = ""
With .Item
.To = "123#321.com"
.CC = ""
.BCC = ""
.Subject = "A new deli pre-order has been received."
.Send
End With
The part where I am now struggling is to set who the email has come from
I thought adding the below would work:
.From = "111#222.com"
What happens when adding the above:
No email is received at all
What am I missing?

You can try .SendUsingAccount to select the account which you are going to send the e-mail.
With .Item
.SendUsingAccount = olAccount 'or some other account.
.To = "123#321.com"
.CC = ""
.BCC = ""
.Subject = "A new deli pre-order has been received."
.Send
End With

You're probably looking for .SenderEmailAddress or .SentOnBehalfOfName.

Try it this way.
Sub Mail_workbook_Outlook_1()
'Working in Excel 2000-2016
'This example send the last saved version of the Activeworkbook
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.to = "ron#debruin.nl"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = "Hi there"
.Attachments.Add ActiveWorkbook.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Send 'or use .Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
https://www.rondebruin.nl/win/s1/outlook/amail1.htm

Related

How to add JPG file at specific location in email body created from a template?

I'm trying to distribute emails using an Outlook oft template.
On the oft template, at a specific location, I want to attach a jpg file which I have created from Excel range.
Dim OutApp As Object
Dim OutMail As Object
Dim str_jpeg_file as String
str_jpeg_file = "B:\temp\test.jpg"
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItemFromTemplate(ThisWorkbook.Path & "\Test.oft")
With OutMail
.To = "test#abcd.com"
.CC = ""
.BCC = ""
.Subject = "Test mail"
.SentOnBehalfOfName = "zyz#abcd"
.Attachments.Add str_jpeg_file, 1, 0
.HTMLBody = Replace(.HTMLBody, "##IMAGE_PLACEHOLDER##", "<img src=""cid:test.jpg""height=520 width=750>")
'.Send
.display
End With
Edit:
jpg file path updated i.e. str_jpeg_file
To give you a full working example:
Option Explicit
Sub test()
Dim OutApp As Object
Dim OutMail As Object
Dim str_jpeg_file As String
str_jpeg_file = "C:\Temp\test.png"
Set OutApp = CreateObject("Outlook.Application")
'Set OutMail = OutApp.CreateItemFromTemplate(ThisWorkbook.Path & "\Test.oft")
'instead of a template I create a new mail
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = "test#abcd.com"
.CC = ""
.BCC = ""
.Subject = "Test mail"
.SentOnBehalfOfName = "zyz#abcd"
.Attachments.Add str_jpeg_file, 1, 0
'first we write some placeholder text so we can replace it
.HTMLBody = "lalala ##IMAGE_PLACEHOLDER## lala"
'replace
.HTMLBody = Replace(.HTMLBody, "##IMAGE_PLACEHOLDER##", "<img src=""cid:test.png""height=256 width=256>")
'.Send
.display
End With
End Sub
Note that I used a new email (no template) because it is easier to show here.
And it works perfectly:
So if it doesn't work for you either your image file is no valid image or you did something else wrong like typos etc. or your template is somehow the issue. Check again with the code above.

Send Outlook Email with some Bold Text

I have code that opens up an Outlook email with the content of the mail body, but none of the format is working.
In the email, it just shows up as a string Like:
<strong>Bold this part</strong>Unbold this text
<b>This text bold</b>Test
I have been searching answers here, but none of them work.
Here is my code so far:
Sub Send_email()
Dim OutApp As Object
Dim OutMail As Object
Dim MailBody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
MailBody = "First line of email" & vbNewLine & vbNewLine & _
"<strong>Bold this part</strong>" & "Unbold this text" & vbNewLine & _
"<b>This text bold</b>" & "Test"
On Error Resume Next
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = "Email Subject"
.Body = MailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Use MailItem.HTMLBody instead of MailItem.Body.
Returns or sets a String representing the HTML body of the specified item. Read/write.
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = "Email Subject"
.HTMLBody = MailBody
.Display 'or use .Send
End With

Nested IFs to send emails vis VBA

I am trying to complete some VBA to send an email to different people based on set criteria(1, 2 or 3).
I can get the email to send on an IF with the first part but cannot get it to include the different criteria.
Dim OutlookApp As Object
Dim OutlookMail As Object
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
If Sheets("Master").Range("D18") = 1 Then
ActiveWorkbook.Save
On Error Resume Next
With OutlookMail
.To = "x1#1.co.uk"
.CC = "x1#1.co.uk"
.BCC = ""
.Subject = "Holiday Response"
.Body = "Hi, please find attached the requested Holiday thank you." & vbNewLine & Signature
.Attachments.Add Application.ActiveWorkbook.FullName
.Send
ElseIf Sheets("Master").Range("D18") = 2 Then
ActiveWorkbook.Save
On Error Resume Next
With OutlookMail
.To = "x2#2.co.uk"
.CC = "x2#2.co.uk"
.BCC = ""
.Subject = "Holiday Response"
.Body = "Hi, please find attached the requested Holiday thank you." & vbNewLine & Signature
.Attachments.Add Application.ActiveWorkbook.FullName
.Send
Else
ActiveWorkbook.Save
On Error Resume Next
With OutlookMail
.To = "x3#3.co.uk"
.CC = "x3#3.co.uk"
.BCC = ""
.Subject = "Holiday Response"
.Body = "Hi, please find attached the requested Holiday thank you." & vbNewLine & Signature
.Attachments.Add Application.ActiveWorkbook.FullName
.Send
End With
Set OutlookMail = Nothing
Set OutlookApp = Nothing
End If
End Sub
Email will send to different people based on the different criteria being met.
Error message currently shows Compile Error: Else without If
You were missing some End With in your code. You can also avoid repeating all these unnecessary lines of code and simplify a little bit. This will work:
Dim OutlookApp As Object
Dim OutlookMail As Object
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
ActiveWorkbook.Save
With OutlookMail
.BCC = ""
.Subject = "Holiday Response"
.Body = "Hi, please find attached the requested Holiday thank you." & vbNewLine & Signature
.Attachments.Add Application.ActiveWorkbook.FullName
If Sheets("Master").Range("D18") = 1 Then
.To = "x1#1.co.uk"
.CC = "x1#1.co.uk"
ElseIf Sheets("Master").Range("D18") = 2 Then
.To = "x2#2.co.uk"
.CC = "x2#2.co.uk"
Else
.To = "x3#3.co.uk"
.CC = "x3#3.co.uk"
End If
'Here use a proper error handling
'On Error Resume Next
.Send
End With
Set OutlookMail = Nothing
Set OutlookApp = Nothing
Note: As already stated, you need to implement a proper error handling in case of failure sending your mail.
Hope this helps.

Send Outlook email using text in an Excel textbox - Error 424: Object required

I am trying to send an email with Outlook using text in a textbox (I named it tx in Excel) as body.
When I run the code, there is an error on the line:
strbody = tx.Text
Error 424: Object required
Sub SendMail()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
strbody = tx.Text
'On Error Resume Next
With OutMail
.To = "..."
.CC = ""
.BCC = ""
.Subject = Cells(3, 2)
.Body = strbody
.Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Replace Sheet's name by the name of the sheet where your textbox is
in strbody = ThisWorkBook.Sheets("Sheet's name").Shapes("tx").ControlFormat.Value
Sub SendMail()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
strbody = ThisWorkBook.Sheets("Sheet's name").Shapes("tx").ControlFormat.Value
'On Error Resume Next
With OutMail
.To = "..."
.CC = ""
.BCC = ""
.Subject = Cells(3, 2)
.Body = strbody
.Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
You could use CDO? Here's some quick VBA code I put in a test function in Excel VBA (with the email addresses and SMTP server address redacted):
Sub test()
Dim strbody As String
strbody = "Test Email" & vbNewLine & vbNewLine & "TEST EMAIL"
Dim iMsg As Object
Set iMsg = CreateObject("CDO.Message")
With iMsg
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") _
= "whateverYourSMTPServerIs"
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") _
= 25
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") _
= 2 'Stands for sending using CDO
.Configuration.Fields.Update
.To = "someemail#someplace.com"
.CC = ""
.BCC = ""
.From = "someemail#someplace.com"
.Subject = "Test Email"
.TextBody = strbody
.Send
End With
End Sub

How to send email to multiple recipients with addresses stored in Excel?

I am trying to set up several buttons on an Excel form to email different groups of people.
I made several ranges of cells on a separate worksheet to list the email addresses.
For example, I want "Button A" to open Outlook and put the list of email addresses from "Worksheet B: Cells D3-D6". Then all that has to be done is hit "Send" in Outlook.
Sub Mail_workbook_Outlook_1()
'Working in 2000-2010
'This example send the last saved version of the Activeworkbook
Dim OutApp As Object
Dim OutMail As Object
EmailTo = Worksheets("Selections").Range("D3:D6")
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = EmailTo
.CC = "person1#email.com;person2#email.com"
.BCC = ""
.Subject = "RMA #" & Worksheets("RMA").Range("E1")
.Body = "Attached to this email is RMA #" & Worksheets("RMA").Range("E1") & ". Please follow the instructions for your department included in this form."
.Attachments.Add ActiveWorkbook.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display
End With
On Error Goto 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
You have to loop through every cell in the range "D3:D6" and construct your To string. Simply assigning it to a variant will not solve the purpose. EmailTo becomes an array if you assign the range directly to it. You can do this as well but then you will have to loop through the array to create your To string
CODE
Option Explicit
Sub Mail_workbook_Outlook_1()
'Working in 2000-2010
'This example send the last saved version of the Activeworkbook
Dim OutApp As Object
Dim OutMail As Object
Dim emailRng As Range, cl As Range
Dim sTo As String
Set emailRng = Worksheets("Selections").Range("D3:D6")
For Each cl In emailRng
sTo = sTo & ";" & cl.Value
Next
sTo = Mid(sTo, 2)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = sTo
.CC = "person1#email.com;person2#email.com"
.BCC = ""
.Subject = "RMA #" & Worksheets("RMA").Range("E1")
.Body = "Attached to this email is RMA #" & _
Worksheets("RMA").Range("E1") & _
". Please follow the instructions for your department included in this form."
.Attachments.Add ActiveWorkbook.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
ToAddress = "test#test.com"
ToAddress1 = "test1#test.com"
ToAddress2 = "test#test.com"
MessageSubject = "It works!."
Set ol = CreateObject("Outlook.Application")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.RecipIents.Add(ToAddress)
newMail.RecipIents.Add(ToAddress1)
newMail.RecipIents.Add(ToAddress2)
newMail.Send
Both answers are correct.
If you user .TO -method then the semicolumn is OK - but not for the addrecipients-method. There you need to split, e.g. :
Dim Splitter() As String
Splitter = Split(AddrMail, ";")
For Each Dest In Splitter
.Recipients.Add (Trim(Dest))
Next

Resources