Morshed_Dhaka
New Member
- Joined
- Dec 16, 2016
- Messages
- 42
Hello Everyone,
Good day to all of you.
I am really struggling to run this VBA code. When I run this code, its perfect did the work which I am looking for but at the end, it shows " Run Time Error '91' Object Variable or With block variable not set "
Below is my code. If anybody correct the code that where is the problem, It will be really helpful for me. Thanks in advance.
VB Code :
Module 01 :
Module 02 :
Good day to all of you.
I am really struggling to run this VBA code. When I run this code, its perfect did the work which I am looking for but at the end, it shows " Run Time Error '91' Object Variable or With block variable not set "
Below is my code. If anybody correct the code that where is the problem, It will be really helpful for me. Thanks in advance.
VB Code :
Module 01 :
Code:
Sub Worksheet_Calculate()
Dim BillPending As Range
Dim MyLimit As Double
MyLimit = 0
Set BillPending = Sheets("APRIL").Range("AN1:AN38")
On Error GoTo EndMacro:
For Each mailid In BillPending.Cells
With mailid
If .Value > MyLimit Then
Call Mail_with_outlook2
End If
Application.EnableEvents = False
Application.EnableEvents = True
End With
Next mailid
ExitMacro:
Exit Sub
EndMacro:
Application.EnableEvents = True
MsgBox "Some Error occurred." _
End Sub
Code:
Option Explicit
Public mailid As Range
Sub Mail_with_outlook2()
Dim OutApp As Object
Dim OutMail As Object
Dim strto As String, strcc As String, strbcc As String
Dim strsub As String, strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail= OutApp.CreateItem(0)
strto = Cells(mailid.Row, "AL").Value
strcc = ""
strbcc = ""
strsub = "Your subject"
strbody = "Hi " & Cells(mailid.Row, "AM").Value & vbNewLine & vbNewLine & _
"Your total of this week is : " & Cells(mailid.Row, "AN").Value & _
vbNewLine & vbNewLine & "Good job"
With OutMail
.To = strto
.CC = strcc
.BCC = strbcc
.Subject = strsub
.Body = strbody
.Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Last edited by a moderator: