Can anyone tell me why I'm getting the "Run-time error '91'" message in my function below? It's happening on this line:
Set olMailItem = olApp.CreateItem(olMailItem)
Also, whenever I'm in debug and I place my cursor over this line, Access gives me this message: "Outlook.Application = < Object variable or With block variable not set >":
Dim olApp As New Outlook.Application
I'm trying to create a button that will open an outlook email message and allow the data entry clerk to edit the message before sending it. I've checked my references and I have Microsoft Outlook 14.0 Object Library checked.
Also, if you have any suggestions on making my code more efficient, please share. I'm fairly new to Access programming.
Private Sub EmailButton_Click()
Dim EmailThis As String
EmailThis = CreateEmailWithOutlook("[email protected]", "Testing e-mail Access database", "This is a test")
DoCmd.SendObject acSendForm, "SubmitNewIdeaForm", , "My Name", , "Test", , True
On Error GoTo CreateEmail_Exit
CreateEmail_Exit:
Exit Sub
End Sub
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 New Outlook.Application
Dim olMailItem As Outlook.MailItem ' An Outlook Mail item
Set olApp = CreateObject("Outlook.Application")
' 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