-1

I am getting the error mentioned in the title while running a program to send emails through outlook. Can anyone please help? It highlights the 2nd line -
enter image description hereDim olApp As Outlook.Application

Code:

Sub send_mail(address As String, subject As String, mail_body As String)
'

'
Dim **olApp As Outlook.Application**
Set olApp = CreateObject("Outlook.Application")
Dim olMail As Outlook.mailitem
Set olMail = olApp.CreateItem(olMailItem)

olMail.To = address
olMail.subject = "Maintenance Activity at: " & subject
olMail.Body = "Equipment to be maintained: " & mail_body
olMail.CC = Sheets("Expiry").Range("M8").Value
olMail.send

End Sub

Sub mass_mail()
n = Now()
MsgBox "date: " & n
If MsgBox("Are You Sure? ", vbYesNo, " Warning! ") = vbYes Then
Dim row_number As Integer
row_number = 4

Do
DoEvents

row_number = row_number + 1
If Range("D" & row_number).Value >= n And Range("D" & row_number).Value < (n + 30) Then
Call send_mail(Sheet1.Range("L" & row_number), Sheet1.Range("C" & row_number), Sheet1.Range("D" & row_number))
MsgBox "Date: " & Range("D" & row_number).Value
End If
Loop Until row_number = 10
MsgBox ("Messages(s) Sent!")
End If
End Sub
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Anas Asif
  • 7
  • 1
  • 2

1 Answers1

1

Early bound library reference:

You need to add a reference to the Outlook library in the project references.

In the visual basic editor you need to go Tools > References and scroll down to find the Microsoft Outlook Object Library for your Office version

For me this is version 16.0

example


Late bound:

Or, you can convert your library call to late bound, which doesn't need a library reference added and is safer when distributing an application to different users who may have different Office versions, with:

Set olApp = CreateObject("Outlook.Application")

Info on Early versus Late Binding:

  1. Early and Late Binding (Visual Basic)
QHarr
  • 83,427
  • 12
  • 54
  • 101