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
Related
I am trying to get email addresses from a worksheet(Sheet1) into the .To line for an outlook email based on the specific string value in the main worksheet.
I have managed to play with it several ways but none have given the results I need. The idea is that it checks a cell on the main worksheet for a specific string value, then would reference a specific range of cells in one column from another worksheet based on the string value and include these emails in the .To line, separated by a";".
I also noticed it removed data from the cells when it pulled it in testing, replacing some cells with "Column1"
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim emailRng As Range, cl As Range
Dim sTo As String
Set emailRng = Worksheets("SHEET1").Range("D3:D20")
For Each cl In emailRng
sTo = sTo & ";" & cl.Value
Next
sTo = Mid(sTo, 2)
If InStr(ActiveCell.Value, "ABC") > 0 Then
emailRng = ThisWorkbook.Sheets("SHEET1").Range("D3:D5")
ElseIf InStr(ActiveCell.Value, "XYZ") > 0 Then
emailRng = ThisWorkbook.Sheets("SHEET1").Range("D11:D15")
End If
If Target.CountLarge > 1 Then Exit Sub
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
Select Case Target.Column
Case Is = 15
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = sTo
.CC = "REQ#EMAIL.COM"
.Subject = ""
.HTMLBody = "Please attend "
.Display
End With
End Select
Application.ScreenUpdating = False
End Sub
First of all, creating a new Outlook Application instance in the Worksheet_BeforeDoubleClick handler is not really a good idea. Consider creating an Outlook instance once and then only create a new email in the event handler instead.
Instead of relying on To or CC properties:
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = sTo
.CC = "REQ#EMAIL.COM"
.Subject = ""
.HTMLBody = "Please attend "
.Display
End With
I'd recommend using the Recipients property of the MailItem class which returns a Recipients collection that represents all the recipients for the Outlook item. For example:
Sub CreateStatusReportToBoss()
Dim myItem As Outlook.MailItem
Dim myRecipient As Outlook.Recipient
Set myItem = Application.CreateItem(olMailItem)
Set myRecipient = myItem.Recipients.Add("Eugene Astafiev")
myItem.Subject = "Status Report"
myItem.Display
End Sub
Then I'd recommend using the Resolve or ResolveAll method which attempts to resolve all the Recipient objects in the Recipients collection against the Address Book.
Sub CheckRecipients()
Dim MyItem As Outlook.MailItem
Dim myRecipients As Outlook.Recipients
Dim myRecipient As Outlook.Recipient
Set myItem = Application.CreateItem(olMailItem)
Set myRecipients = myItem.Recipients
myRecipients.Add("Eugene Astafiev")
myRecipients.Add("Dmitry Anafriev")
myRecipients.Add("Tom Wilon")
If Not myRecipients.ResolveAll Then
For Each myRecipient In myRecipients
If Not myRecipient.Resolved Then
MsgBox myRecipient.Name
End If
Next
End If
End Sub
You may find the How To: Fill TO,CC and BCC fields in Outlook programmatically article helpful.
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
I have code to send emails to a mailing group from within Excel.
The group (*.msg outlook contact file) is in a shared drive folder and is constantly updated.
I normally manually delete the group contact from my Outlook's "People" tab then drag the updated file into the tab.
Can I automate loading the contact group from the shared drive folder, creating the email, then deleting the group contact?
Or, can I automate reading the group contact list and copying the addresses into the "To" field without loading/deleting the contact group into Outlook?
Sub CreateReportEmail()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Dim BodyString As String
BodyString = "Body of email"
On Error Resume Next
With OutMail
.To = **MailingGroup**
.Subject = "Bi-weekly report"
.Body = "Body of email"
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
It sounds like you have a text file containing the intended recipients of the email message. If that's the case, you don't necessarily need to worry about creating a contact group in Outlook: You can just open the file, pull the recipients, and add it to the To of your email. My suggestion would be to encapsulate the code to get your recipients, so your final code might look something like this:
Sub CreateReportEmail()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Dim BodyString As String
BodyString = "Body of email"
On Error Resume Next
With OutMail
.To = GetMailingGroup
.Subject = "Bi-weekly report"
.Body = "Body of email"
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Function GetMailingGroup() As String
Dim distList As Outlook.DistListItem
Dim oApp As Outlook.Application
Dim emailArray() As String
Dim i As Integer
Set oApp = Outlook.Application
Set distList = oApp.CreateItemFromTemplate("\\nasfsu01\ReDirFold$\RedirectedFolders$\zthurst\Downloads\SHSC Member Services Bilingual Associates.msg")
ReDim emailArray(1 To distList.MemberCount)
For i = 1 To distList.MemberCount
emailArray(i) = distList.GetMember(i).Address
Next i
GetMailingGroup = Join(emailArray, ";")
End Function
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