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
Related
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
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)
An Outlook email is generated whenever I execute a VBA code in Excel. It does not automatically send, nor do I want it to. The email is populated by cell values in a range (which are based off of the ActiveCell) and I want to programmatically capture when the email is manually sent into ActiveCell.Offset(0, 13), preferably with VBA in my current Excel program.
This is the code by which I display the email:
'Send Stock Request:
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.BodyFormat = olFormatHTML
.HTMLBody = "My eMail's HTML Body"
.To = "myrecipients#theiremails.com"
.CC = ""
.BCC = ""
.Subject = "Stock Request"
.Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
It can be done through VBA, but code below must be pasted in Outlook module instead of Excel, in Outlook=>ThisOutlookSession module. Also, make sure you allow macros in Outlook.
Private Sub Application_ItemSend(ByVal olItem As Object, Cancel As Boolean)
Dim Xl As Object ' Excel.Application
Dim Wb As Object ' Excel.Workbook
Set Xl = GetObject(, "excel.application")
Set Wb = Xl.Workbooks("NameOfYourOpenedWorkbook.xlsb")
Wb.Activate
Xl.activecell.Offset(0, 13).Value = Date & " " & Time
End Sub
So now when you send your automatically created email manually, you will get date and time captured in your opened Workbook in ActiveCell.Offset(0, 13) cell.
Add a VBA project reference to the Outlook object model, and add this class to your excel file:
''clsMail
Option Explicit
Public WithEvents itm As Outlook.MailItem
Public DestCell As Range '<< where to put the "sent" message
'you can add other fields here if you need (eg) to
' preserve some other info to act on when the mail is sent
Private Sub itm_Send(Cancel As Boolean)
Debug.Print "Sending mail with subject: '" & itm.Subject & "'"
DestCell.Value = "Mail sent!" '<< record the mail was sent
End Sub
Then in your Mail-sending code you can do something like this:
Option Explicit
Dim colMails As New Collection
Sub Tester()
Dim OutApp As Object
Dim OutMail As Object
Dim obj As clsMail
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.BodyFormat = olFormatHTML
.HTMLBody = "My eMail's HTML Body"
.To = "twilliams#theravance.com"
.CC = ""
.BCC = ""
.Subject = "Stock Request"
.Display
End With
'create an instance of the class and add it to the global collection colMails
Set obj = New clsMail
Set obj.itm = OutMail
Set obj.DestCell = ActiveCell.Offset(0, 13) '<< "sent" flag goes here
' when the user sends the mail
colMails.Add obj
End Sub
I would like to retrieve the email addresses from excel cells and copy them as recipients on outlook.
However, the "To" and "CC" on outlook are empty.
input and output:
Cell A1 is the email address which I want to "send to".
Cell A2 is the email address which I want to "CC to".
my VBA code:
Sub Button1_Click()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
On Error Resume Next
With OutMail
.To = Cells("A1")
.CC = Cells("A2")
.BCC = ""
.Subject = "This is the Subject line"
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
If you remove "On Error Resume Next" you can debug. The following are invalid:
.To = Cells("A1")
.CC = Cells("A2")
Try
.To = Range("A1")
.CC = Range("A2")
You need to add a recipient, not the To, CC or BCC properties. These properties contain the display names only. The Recipients collection should be used to modify this property. For example:
Sub CreateStatusReportToBoss()
Dim myItem As Outlook.MailItem
Dim myRecipient As Outlook.Recipient
Set myItem = Application.CreateItem(olMailItem)
Set myRecipient = myItem.Recipients.Add("Dan Wilson")
myItem.Subject = "Status Report"
myItem.Display
End Sub
You may find the following articles helpful:
How To: Create and send an Outlook message programmatically
How To: Fill TO,CC and BCC fields in Outlook programmatically
I have had better luck with a Recipient:
'If not defined:
'olBCC=3
'olCC=2
'olTo=1
Set OutMail = Application.CreateItem(olMailItem)
Set myRecipient = OutMail.Recipients.Add(Range("A1"))
'myRecipient.Type = olTo
'This is default - use for clarity if desired
Set myRecipient = OutMail.Recipients.Add(Range("A2"))
myRecipient.Type = olCC
If you wish to add multiple recipients, you will have to add them one at a time
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