Error received: User-defined type not defined on
Dim oapp As Outlook.Application
Desired result: I have 2 tabs Sheet 1 and Email tab
I wish to take a screenshot of cells B8 TO M108 and send it to my desired recipients in the email body
I have the following code. I am not sure how to proceed.
Could I lend some help?
Sub sendemail()
Application.ScreenUpdating = False
Dim oapp As Outlook.Application
Dim email As Outlook.MailItem
Set oapp = New Outlook.Application
Set email = oapp.CreateItem(olMailItem)
email.To = Worksheets("Email").Range("A10").Value
email.CC = Worksheets("Email").Range("B10").Value
email.HTMLBody =
email.Subject = "Snapshot"
email.Display True
End Sub
Late binding could help.
dim oapp as object, email as object
set oapp = createobject("outlook.application")
set email = oapp.createitem(0)
with email
.To = Worksheets("Email").Range("A10").Value
.CC = Worksheets("Email").Range("B10").Value
.HTMLBody = ""
.Subject = "Snapshot"
.Display
End With
set oapp = Nothing
set email = Nothing
In Tools/Preferences...
add Microsoft Outlook ##.# Object Library.
Just check it.
##.# - Number of your office.
Related
I have code that sends emails using the default Outlook account.
I tried changing the code to send from a specific email. When I run the macro, nothing happens.
Is something wrong with the code, or is it not working due to another issue (with Outlook and the accounts/permissions associated with it)?
Sub CommandButton1_Click()
Dim wb As Workbook
Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
Dim q As Long
Dim oAccount As Outlook.Account
Set wb = ThisWorkbook
For Each oAccount In Outlook.Application.Session.Accounts
If oAccount = "theEmailiWantToUse#domain.com" Then
For q = 2 To 3 'LastRow
eName = wb.Sheets(1).Cells(q, 2).Value
Set olApp = New Outlook.Application
Set olMail = olApp.CreateItem(olMailItem)
mailBody = "Hello, "
With olMail
.To = Worksheets("Emails").Cells(q, 4).Value
.Subject = eName
.HTMLBody = "<!DOCTYPE html><html><head><style>"
.HTMLBody = .HTMLBody & "body{font-family: Calibri, ""Times New Roman"", sans-serif; font-size: 14px}"
.HTMLBody = .HTMLBody & "</style></head><body>"
.HTMLBody = .HTMLBody & mailBody & "</body></html>"
Set .SendUsingAccount = oAccount
.Display
' .Send
End With
Next
Else
End If
Next
Set olMail = Nothing
Set olApp = Nothing
End Sub
I know I have access to the email I would like to send emails from, as I can select it from Outlook and it works.
Add this line within the olMail
.SentOnBehalfOfName = "youraddress" 'here change this
please use this routine to find Account number of sender .
Sub Which_Account_Number()
'Don't forget to set a reference to Outlook in the VBA editor
Dim OutApp As Outlook.Application
Dim I As Long
Set OutApp = CreateObject("Outlook.Application")
For I = 1 To OutApp.Session.Accounts.Count
MsgBox OutApp.Session.Accounts.Item(I) & " : This is account number " & I
Next I
End Sub
Then
.SendUsingAccount = olApp.Session.Accounts.Item(5)' whatever account index number you want to send. i have chosen 5
instead of
Set .SendUsingAccount = oAccount
This method works for me . You can further integrate this concept in your programme. Please ensure Reference to Outlook Object Library is set in Tools/References.
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 want to make a simple program in which I can create an Outlook Task. User fills in data and clicks the create button.
I found the following code online and it should work, but it doesn't. No task is added in my Outlook, but no error is shown either. I have the feeling it somehow goes wrong with adding the recipients of the task.
Any clue why I don't get an error but, no tasks are added?
Dim OutApp As Outlook.Application
Dim OutTask As Outlook.TaskItem
Set OutApp = CreateObject("Outlook.Application")
Set OutTask = OutApp.CreateItem(olTaskItem)
Set myRecipient = OutTask.Recipients.Add("I.wont.write.my.actual.address.in.this#example.com")
myRecipient.Type = olTo
If myRecipient.Resolved Then
With OutTask
.Display
.Subject = Cells(3, "I")
.StartDate = Now
.DueDate = Cells(2, "I")
.Body = "Please see the attached email for a service request assigned to you."
End With
End If
Set OutTask = Nothing
Set OutApp = Nothing
I just can't figure it out and it's really breaking my brain at the moment. Hope someone can hint me in the right direction!
I found the following code online and it should work, but it doesn't. No task is added in my Outlook, but no error is shown either. I have the feeling it somehow goes wrong with adding the recipients of the task.
Correct - Attempt to resolve the Recipient object myRecipient.Resolve against the Address Book before assuming its resolved If myRecipient.Resolved Then also defined variable Dim myRecipient As Outlook.Recipient for myRecipient
Option Explicit
Sub tasks()
Dim OutApp As Outlook.Application
Set OutApp = CreateObject("Outlook.Application")
Dim OutTask As Outlook.TaskItem
Set OutTask = OutApp.CreateItem(olTaskItem)
Dim myRecipient As Outlook.Recipient
Set myRecipient = OutTask.Recipients.Add("0m3r#Email.com")
myRecipient.Type = olTo
myRecipient.Resolve
If myRecipient.Resolved Then
With OutTask
.Display
.Subject = Cells(3, "I")
.StartDate = Now
.DueDate = Cells(2, "I")
.Body = "Please see the attached email."
End With
End If
Set OutTask = Nothing
Set OutApp = Nothing
End Sub
Option Explicit Statement (Visual Basic)
Forces explicit declaration of all variables in a file, or allows implicit declarations of variables.
Check this examples, run each one and see if they are usefull to you, hope one fit your needs:
Sub outlook_send_followup()
' High importance = 2
' Nothing = 1
' Low importance = 0
Dim OutApp As Object
Set OutApp = CreateObject("Outlook.Application")
Dim MyItem As Object
Set MyItem = OutApp.CreateItem(olMailItem)
With MyItem
.To = "example#hotmail.com"
.Subject = "hi, this is a task"
.SentOnBehalfOfName = "example#hotmail.com"
.HTMLBody = "<HTML MSG FORMAT HERE>"
.Importance = 1
.FlagStatus = olFlagMarked
.FlagRequest = "Follow up"
.FlagDueBy = Now
.Display
End With
Set MyItem = Nothing
Set OutApp = Nothing
End Sub
Sub create_outlook_taks()
'Const olImportanceLow = 0
'Const olImportanceNormal = 1
'Const olImportanceHigh = 2
Dim outlook_app As Object
Set outlook_app = CreateObject("Outlook.Application")
With outlook_app.CreateItem(3)
.Importance = 2
.Subject = "THIS IS A TASK"
.StartDate = Now + 5
.DueDate = Now + 10
.ReminderTime = Now - 3
.Body = "HI YOU CREATED THIS TASK"
.Display
'.Save
End With
Set outlook_app = Nothing
End Sub
I am creating emails from Excel via the VBA Outlook.Application Reference. Each email is populated with data from my excel sheet and then placed into the To/CC/BCC/Subject/Body fields.
Now, when running this code in Office 2010 it works without a hitch, but in Office 2013 the variables containing the To/CC/BCC/etc. data does not show up in the actual email when displayed.
Did this reference change in Office 2013?
Sub MailSheet()
Dim OutApp As Object
Dim outMail As Object
Dim rng As Range
' set required variables
Set Sourcewb = ActiveWorkbook
Set Property = ActiveWorkbook.Sheets("Settings").Range("B4")
Set Holidex = ActiveWorkbook.Sheets("Settings").Range("B5")
Set SendTo = ActiveWorkbook.Sheets("Settings").Range("B29")
Set SendCC = ActiveWorkbook.Sheets("Settings").Range("B30")
Set rng = Sheets("Mail").Range("A1:F80")
' set email variables
Set OutApp = CreateObject("Outlook.Application")
Set outMail = OutApp.CreateItem(0)
' some code
' get ready to mail
With outMail
.To = SendTo
.ReplyRecipients.Add ""
.CC = SendCC
.BCC = ""
.Subject = Holidex & " - Daily Email"
.HTMLBody = RangetoHTML(rng)
' display email before sending
.Display '.Send or use .Display
End With
' some code
' Clean up
Set outMail = Nothing
Set OutApp = Nothing
end Sub
Rather than creating an Outlook object, try referencing the outlook library (Tools -> References and then select Microsoft Outlook xx.x Object Library). You can then reference it as below:
Sub SendAnEmail()
Dim oOlApp As Outlook.Application: Set oOlApp = Outlook.Application
Dim oMailItem As Outlook.MailItem: Set oMailItem = oOlApp.CreateItem(olMailItem)
oMailItem.To = "myemail#test.com"
oMailItem.CC = ""
oMailItem.BCC = "myemail#test.com"
oMailItem.Subject = Sheet1.Cells(15, "D")
oMailItem.HTMLBody = "Again .. testing"
oMailItem.Display
Set oMailItem = Nothing
Set oOlApp = Nothing
End Sub
You can either add this code in your sub or call this Sub from your Sub with parameters
Not sure I can help you directly, but I do have some code I found online which I know for a fact works with Outlook 2016, will share it on here in case it helps:
Sub OutlookMail_1()
'Automate Sending Emails from Excel, using Outlook.
'Send text and also contents from the host workbook's worksheet range
' as Mail Body, and add an attachment with the mail.
'Automating using Early Binding: Add a reference to the Outlook Object Library
' in Excel (your host application) by clicking Tools-References in VBE,
' which will enable using Outlook's predefined constants.
'Once this reference is added, a new instance of
' Outlook application can be created by using the New keyword.
'variables declared as a specific object type
' ie. specific to the application which is being automated:
Dim applOL As Outlook.Application
Dim miOL As Outlook.MailItem
Dim recptOL As Outlook.Recipient
Dim ws As Worksheet
Dim name As String
Dim email As String
Dim nominees As Range
Dim number As String
'set worksheet:
Set ws = ThisWorkbook.Sheets("Sheet1")
'Create a new instance of the Outlook application.
' Set the Application object as follows:
Set applOL = New Outlook.Application
'create mail item:
Set miOL = applOL.CreateItem(olMailItem)
'Add mail recipients, either the email id or their name in your address book.
' Invalid ids will result in code error.
Set recptOL = miOL.Recipients.Add("Main recipient email")
recptOL.Type = olTo
Set recptOL = miOL.Recipients.Add("BCC Email")
recptOL.Type = olbcc
Set recptOL = miOL.Recipients.Add("BCC Email")
recptOL.Type = olbcc
Set recptOL = miOL.Recipients.Add("BCC Email")
recptOL.Type = olbcc
'with the mail item:
With miOL
'subject of the mail:
.Subject = "Subject"
'Chr(10) represents line feed/new line, & Chr(13) represents carriage return.
' Send text and also contents from
' the host workbook's worksheet range as Mail Body.
.Body = "BODY OF EMAIL"
'set importance level for the mail:
.Importance = olImportanceHigh
'add an attachment to the mail:
'send the mail:
.Display
End With
'clear the object variables:
Set applOL = Nothing
Set miOL = Nothing
Set recptOL = Nothing
End Sub
Some of the variables I set are redundant because I edited the code slightly to maintain privacy, but let me know if that helps!
If you want to use CC instead of Bcc, then just change the code to:
recptOL.Type = olcc
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