Problem sending emails from access

tozey

Registered User.
Local time
Today, 08:46
Joined
Apr 27, 2010
Messages
23
Hi all.

Pulling my hair out with this.

I have 2 PC's one running Win XP and one running Win 7

Both have Office 2010

I can run the following code with no problem on the PC running XP, however on the Win 7 PC I get an error, details below

Code:
Sub sendOutlookEmail()
Dim oApp As Outlook.Application
Dim oMail As MailItem
Set oApp = CreateObject("Outlook.application")

Set oMail = oApp.CreateItem(olMailItem)
oMail.Body = "Body of the email"
oMail.Subject = "Test Subject"
oMail.To = "Someone@somewhere.com"
oMail.Send
End Sub

This is a simplified verison of the code for the example.

The error on the Windows 7 PC occurs at the Omail.Send part. I get the following error:

Run-time error '287':
Application-defined or object-defined error

Both systems are running the following references.
Office 14 Object Library
Access 14 Object Library
Outlook 14 Object Library
(plus a few others)

Outlook is open when trying to run the code.

I am clueless and am close to having to resort to a mailmerge which I really dont want to do.

Really appreciate any advice.

Thanks

Alex
 
Nothing is getting created, so do you have Outlook loaded in the vbe menu: tools, references?
 
hi Ranman256

I have Outlook 14 Object Library reference ticked as well as others (listed above)

Not sure of anything else I need to do, again it works on the other system?

cheers

Alex
 
Try

Code:
Dim oApp As Object
Dim oMail As Object

Set oApp = CreateObject("Outlook.Application")
oApp.Session.Logon
Set oMail = OutApp.CreateItem(0)

Instead of early binding
 
Sorry Minty

My knowledge is limited and "Early Binding" doesnt really mean anything to me lol :confused:

I am one of those guys that copy and pastes stuff I find and pick it up as I go along, excuse my ignorance. :) one day, I might have some proper training :banghead:

So I tried this

Code:
Sub sendOutlookEmail()
Dim oApp As Object
Dim oMail As Object
Set oApp = CreateObject("Outlook.Application")
oApp.Session.Logon
Set oMail = OutApp.CreateItem(0)
oMail.Body = "Body of the email"
oMail.Subject = "Test Subject"
oMail.To = "Someone@somewhere.com"
oMail.Send
End Sub

Set oMail = OutApp.CreateItem(0) this line is throwing an object required error

Thanks for your help, any further assistance would be amazing.

cheers

Alex
 
Just tried this but getting same runtime error at the .send part

Code:
Sub sendOutlookEmail()
Dim oApp As Object
Dim oMail As Object
Set oApp = CreateObject("Outlook.Application")
oApp.Session.Logon
Set oMail = oApp.CreateItem(olMailItem)
oMail.Body = "Body of the email"
oMail.Subject = "Test Subject"
oMail.To = "Someone@somewhere.com"
oMail.Send
End Sub
 
Sorry Minty

My knowledge is limited and "Early Binding" doesnt really mean anything to me lol :confused:

I am one of those guys that copy and pastes stuff I find and pick it up as I go along, excuse my ignorance. :) one day, I might have some proper training :banghead:

So I tried this

Code:
Sub sendOutlookEmail()
Dim oApp As Object
Dim oMail As Object
Set oApp = CreateObject("Outlook.Application")
oApp.Session.Logon
Set oMail = [COLOR="Red"]oApp[/COLOR].CreateItem(0)
oMail.Body = "Body of the email"
oMail.Subject = "Test Subject"
oMail.To = "Someone@somewhere.com"
oMail.Send
End Sub

Set oMail = OutApp.CreateItem(0) this line is throwing an object required error

Thanks for your help, any further assistance would be amazing.

cheers

Alex

Sorry typo in the red !
 
thanks for the reply Minty

Unfortuantely its throwing the same error at the .send stage

Run-time error '287':
Application-defined or object-defined error

Really dont know why it would work on one OS and not another.

Appreciate you taking the time to look
 
Here is my complete HTML email code - this works on Windows 8 & 10 with Full version access 2010, and runtime 2010 and runtime 2013.

Code:
    Dim sMsgSave        As String
    Dim sEmailAdd       As String
    Dim Variable_To     As String
    Dim Variable_Subject As String
    Dim Variable_Body   As String
    Dim signature       As String
    Dim OutApp          As Object
    Dim OutMail         As Object

    sMsgSave = "Set your message string in here!"        [COLOR="Green"] 'Mines pulled from a table and then formatted to look better in the email[/COLOR]
    Variable_To = sEmailAdd
    Variable_Subject = "Re: Query on service call " &  me.ServiceCallID    [COLOR="Lime"][COLOR="Green"]'Replace crlf with <b> in the html otherwise it looks rubbish[/COLOR][/COLOR]
    sMsgSave = Replace(sMsgSave, vbCrLf, "<br>")

    Variable_Body = "<br><style> " & _
                    "p {font-size:11pt; font-family:Calibri; color:Blue}" & _
                    "</style>" & _
                    "<p>" & sMsgSave & "<br/>"

    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
    On Error Resume Next
    With OutMail                               [COLOR="green"]' This creates a blank email and captures the users default signature.[/COLOR]
        .BodyFormat = 2                        [COLOR="green"]' 2 = HTML format[/COLOR]
        .Display
    End With

    signature = OutMail.HTMLBody

    With OutMail

        .To = Variable_To
        .CC = ""
        .BCC = ""
        .Subject = Variable_Subject
        '.Attachments.Add (sPath)
        .HTMLBody = Variable_Body & signature
        .Display                              [COLOR="green"] 'or use .Send[/COLOR]
        .ReadReceiptRequested = False
    End With
End Sub
 
Thank Minty, I shall apply and let you know how I get on.

Regards

Alex
 
the plot thickens

Applied the code, changed the Variable_To and Variable_subject to my chosen address and subject

replaced the .display with .send near the bottom

WinXP, the email opens and sends no problem

Win7, the email opens, the relevant fields are updated however stepping through, it gets to .send, doesnt throw an error however it doesnt send the email, does nothing, nadda, not a thing.

finishes happily, no email sent!

I have tried another Win7 PC as well and getting the same result.
 
Weird - I'm pretty certain I've used the same code for ages so it must have been working on Win 7 Machines... We have only recently moved numerous machines on from Win 7 or 8 to 10.

Comment out the on error resume bit, and see what it comes up with and also change it to .display rather than .send
 
Thanks Minty

the .display works fine, brings the email up with the data provided.


removing the error resume shows that the error is the same as before at .send

Run-time error '287':
Application-defined or object-defined error

I ultimately want to loop this and send out a batch of emails at a time, so the .send is vital.....getting closer to the mail merge :(

Really appreciate the help.

Thank you
 
Have you tried this with outlook closed or open. I vaguely remember having to do something weird to remove a security warning when sending immediately...
 
Thanks Minty

The result is the same whether Outlook is opened or closed, if closed it takes a few seconds to display the email, however when it comes to send, same result.

I have a feeling it has something to do with the security set up, again, works fine on XP system....grrrrrrrrr..:banghead:

Thanks again for your efforts.
 
In that case the problem is the security warning that comes up - you need to do some ninja work to get round.

Everything you need is here: http://www.contextmagic.com/express-clickyes/free-version.htm Scroll down to the vba code example.

If you get stuck I do have a working example of this but is will take a significant amount of editing to show you it in a simple context.
 
Thanks Minty, I really appreciate the help.

If I succeed I shall report back :)

All the best

Alex
 

Users who are viewing this thread

Back
Top Bottom