VBA to send Email from Outlook in Background

cstickman

Registered User.
Local time
Today, 14:29
Joined
Nov 10, 2014
Messages
109
Hello everyone, I am trying to take a form and send it in the background using outlook. It basically sends a table to the user when an update has been made. Below is the code

<code>
Private Sub cmdsend_Click()
Dim oApp As New Outlook.Application
Dim oEmail As Outlook.MailItem
Set oApp = CreateObject("Outlook.application")

Set oEmail = oApp.CreateItem(olMailItem)

oEmail.To = Me.txtto
oEmail.Subject = Me.txtsubject
oEmail.Body = Me.txtbody

.Send

oApp = Nothing
oEmail = Nothing

End Sub
</code>

I have tried oEmail.Send and .Send and nothing is working. If I use .send I get an expression on click you entered as the event property setting produced the following error: Invalid or unqualified reference.

If I use oEmail.Send I get this error - Run-Time error '287':
Application-defined or object-defined error.

Then I see the outlook icon in the task bar and then goes away and nothing is sent or received. I am not sure if our Admin has locked down something or what, but I am not sure what to do to figure it out.

I have also referenced the Microsoft Outlook 15.0 Object Library

Any help or suggestions would be greatly appreciated. Thanks!!
 
You're getting the first error on .Send because you're using it without a reference. You need oEmail.Send. You can only leave off the object if you use With and End With.

You'll also bomb on the next two lines because both of them need 'Set' in front of them.

Error 287 is a user-defined error. Something elsewhere in your code is raising it.

Oh, and for a code block here, use square brackets, not < and >.

Edit: One thing I noticed: You're opening a new instance of outlook. Are they normally not running it?

Edit II: It looks like 287 may be an outlook-specific error that's not providing a description.
 
Insert the red parts

oEmail.Save
oEmail.Send
Set oApp = Nothing
Set oEmail = Nothing
 
I have a working email procedure you can use on post number 9 HERE, but it does require that Outlook already be running. Feel free to pillage it.
 
Thank you everyone for your input. It appears our security policy has blocked applications from using outlook to prevent the spread of viruses and spam. I spoke with another programmer on our team and they have ran into the same issue.

Frothingslosh - I will try your code and see if that makes a difference. The majority of the users have email opened so it should not be an issue.
 

Users who are viewing this thread

Back
Top Bottom