Getting error as variable not defined for olMailItem in below VBA

L

Legacy 432851

Guest
Can anyone gives a cue what's the error / how to get out of it.

Option Explicit


Private Sub CommandButton1_Click()
On Error GoTo ErrHandler

' SET Outlook APPLICATION OBJECT.
Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")

' CREATE EMAIL OBJECT.
Dim objEmail As Object
Set objEmail = objOutlook.CreateItem(olMailItem)

With objEmail
.To = "abcd@gmail.com"
.CC = "efgh@gmail.com"
.BCC = "wxyz@gmail.com"
.Subject = "SALE DATED" & Date
.Body = "Hi there"
.Attachments.Add ("C:\Users\NARESH\Desktop\Sale.doc")
.Send
End With

' CLEAR.
Set objEmail = Nothing: Set objOutlook = Nothing

ErrHandler:
'
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Try changing this line:
Set objEmail = objOutlook.CreateItem(olMailItem)

to

Set objEmail = objOutlook.CreateItem(0)

source:
https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/olitemtype-enumeration-outlook
 
Upvote 0

Forum statistics

Threads
1,217,486
Messages
6,136,921
Members
450,032
Latest member
lant69

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top