I am trying to create a new email with access. I have the following code and i keep getting the runtime error 91 "Object variable or With block variable not set" on the line -- Set olMailItem = olApp.CreateItem(olMailItem) -- this code is in a module. i am calling this code from a form and an on-click event of a button. i believe it is something simple but i cannot figure it out. any help is greatly appreciated. thank you very much in advance. -Walker
Code:
Private Sub Command5_Click()
Call CreateEmailWithOutlook("nitewalk@gmail.com", "Test", "TestTest")
End Sub
here is the module code:
Code:
Option Compare Database
Option Explicit
Public Function CreateEmailWithOutlook( _
MessageTo As String, _
Subject As String, _
MessageBody As String)
' Define app variable and get Outlook using the "New" keyword
Dim olApp As Outlook.Application
Dim olMailItem As Outlook.MailItem 'An Outlook Mail Item
'Create a new email object
Set olMailItem = olApp.CreateItem(olMailItem)
'Add the To/Subject?Body to the message and display the message
With olMailItem
.To = MessageTo
.Subject = Subject
.Body = MessageBody
.Display 'To show the email message to the user
End With
'Release all object variables
Set olMailItem = Nothing
Set olApp = Nothing
End Function