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

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
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,443
Messages
6,136,661
Members
450,023
Latest member
ikiwawan

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