Auto Email Send from outlook using excel information from the cell - excel

I need to send auto email from Excel using Excel Outlook, I was trying coding but unable to do it. I have attached sheet for your reference.
Sub SendEmail()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = "ABC#gmail.com"
.CC = ""
.BCC = ""
.Subject = "Report"
.Body = "Hello!"
.Attachments.Add ActiveWorkbook.FullName
.Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
Application.OnTime TimeValue("17:00:00"), "SendEmail"
End Sub

You will need to reference and login into the Outlook.Namespace before creating the email.
Try adding this to to your code:
Set OutApp = CreateObject("Outlook.Application")
'** -> add this block here
Dim OutNS as Object
Set OutNS = OutApp.GetNamespace("MAPI")
OutNS.Logon
'**
Set OutMail = OutApp.CreateItem(0)

Related

Copy-Paste hyperlinks from Excel to Outlook but display custom text

I use VBA to copy-paste a pivot table from Excel to Outlook. One of the columns is links to certain pages.
I would like to customize it with "Click here" and the hyperlink (instead of sharing the long link).
Alber!
If you are using HTMLBody in your construction Outlook you just need to add an "A" tag.
Sub Send_Email()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
strHTML = "<a href='http://www.google.com.br'>Click Here</a>"
With OutMail
.To = "destinatario#gmail.com"
.CC = "copiado#gmail.com"
.BCC = "somentesenecessario#gmail.com"
.HTMLBody = strHTML
.Subject = "Test e-mail"
.Display
End With
Set OutApp = Nothing
Set OutMail = Nothing
End Sub

Attaching an Outlook email item and an excel workbook into my email

I had already code for attaching the excel workbook. I just need code for attaching an email item into the email.. please assist
Try below code (change the sheet name and range as per your requirements)
Sub Mail()
Dim r As Range
Set r = Worksheets("to_Mail").Range("A1:AD69")
r.Copy
Dim OutApp As Object
Dim outMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set outMail = OutApp.CreateItem(0)
On Error Resume Next
With outMail
.HTMLBody = activeMailMessage.HTMLBody
.To = ""
.CC = ""
.BCC = ""
.Subject = "Report Complete"
Dim wordDoc As Word.document
Set wordDoc = outMail.GetInspector.WordEditor
wordDoc.Range.PasteAndFormat wdChartPicture
outMail.send
End With
End Sub

How to send TextBox contents as an email body?

I am trying to send an email by pressing a button. Everything works fine other than the body of the email. It always shows up blank. The body is being typed into TextBox1 (NOT User Form). I was hoping that putting it in a textbox would format it when sending the email.
Sub Email()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim ChartName As String
Set OutApp = CreateObject("Outlook.Application")
ChartName = Environ$("temp") & "\Chart4.gif"
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
ActiveWorkbook.Worksheets("Send Email").ChartObjects("Chart 4").Chart.Export _
Filename:=ChartName, FilterName:="GIF"
With OutMail
.To = Range("A2")
.CC = ""
.BCC = ""
.Subject = Range("A4")
'.body is the only thing not working it always shows up blank
.Body = Worksheets("Send Email").TextBox1.Value
.Attachments.Add ChartName
.Display 'will use send when working
End With
On Error GoTo 0
Kill ChartName
Set OutMail = Nothing
Set OutApp = Nothing
End Sub

SentOnBehalfOf not working in Excel 2010 VBA Code

I am working on a VBA script for mailing through Outlook in Excel 2010. Everything runs fine with one exception: the .SentOnBehalfofName line will not work. Here is the complete code
Sub Mail()
' Working in Office 2010-2013
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String ' This is for the Body of the email
Dim signature As String ' This is for the email signature
On Error Resume Next
'Set OutMail = Nothing
'Set OutApp = Nothing
Dim sh As Worksheet
Set sh = Sheets("Mail")
strbody = sh.Range("C9").Value
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail ' This inserts the email signature
.Display
End With
signature = OutMail.HTMLBody
With OutMail
'.Display
.To = sh.Range("C5")
.CC = sh.Range("C6")
.BCC = sh.Range("C7")
.Subject = sh.Range("C8").Value
.HTMLBody = "<br>" & strbody & fncRangeToHtml(sh.Range("C13").Value, sh.Range("C14").Value) & signature
.SentOnBehalfOfName = sh.Range("C4").Value
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
If I remove this section the .SentOnBehalfOf works, but I lose my signature line:
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail ' This inserts the email signature
.Display
End With
signature = OutMail.HTMLBody
If I put this back in the code, I get my signature line back, but I lose my ability to send on behalf of another party.
I'm looking for a solution that allows me to do both. Any help would be appreciated.
Here is my solution. I needed to move the .SentOnBehalfOfName to the first statement in the WITH Command, then .Display immediately after that. I replace the string for signature line with .HTMLBody to pull in the signature line. Code runs fine now!
I don't know why the statements need to be in this order, but it works.......
Sub Mail()
' Working in Office 2010-2013
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String ' This is for the Body of the email
On Error Resume Next
'Set OutMail = Nothing
'Set OutApp = Nothing
Dim sh As Worksheet
Set sh = Sheets("Mail")
strbody = sh.Range("C9").Value
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.SentOnBehalfOfName = sh.Range("C4")
.Display
.To = sh.Range("C5")
.CC = sh.Range("C6")
.BCC = sh.Range("C7")
.Subject = sh.Range("C8").Value
.HTMLBody = "<br>" & strbody & fncRangeToHtml(sh.Range("C13").Value, sh.Range("C14").Value) & .HTMLBody
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub

Reference cell in another workbook as email address in .To field

I'm trying to reference cell A1 in another workbook, in a certain sheet, to be set as the "To" field in an email. Here is my code:
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Addresses = Workbooks("Test.xlsx").Sheets("Sheet2").Range("A1").Value
On Error Resume Next
With OutMail
.to = Addresses
.CC = ""
.BCC = ""
.Subject = "Confirm " & Format(Date, "mm.dd.yy")
.body = "Please see attached for your confirm. Thanks,"
.Attachments.add ActiveWorkbook.FullName
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
When I execute the macro, the "To" field in the email has nothing in it. The cell I am referencing definitely has a value in it. Does anyone have any suggestions?
Try moving display to the beginning. So...
With OutMail
.display

Resources