运行时错误287.在Access中使用VBA通过Outlook发送电子邮件. [英] Runtime error 287. Sending Emails through Outlook using VBA in Access.

查看:225
本文介绍了运行时错误287.在Access中使用VBA通过Outlook发送电子邮件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,一个用户希望收到有关何时将交货添加到Access数据库的通知.我想到的最简单的方法是通过Outlook 2010设置自动电子邮件.

I have a case where one user would like to receive notifications of when deliveries are added to an Access Database. The simplest method I could think of was to set up an automated email via Outlook 2010. The following code is what I have in place:

Dim oApp As Outlook.Application
Dim oMail As MailItem
Dim varDnoteRef2 As String
Dim varuser As String
varuser = DLookup("[Employee_Name]", "employees", "[Employee_ID]=" & TempVars!gloggedin)
varDnoteRef2 = DLast("Supplier_Dnote_Ref", "Supplier_Dnotes")

Set oApp = CreateObject("Outlook.application")
Set oMail = oApp.CreateItem(olMailItem)
oMail.Body = "A Delivery Note has been added to the Database by " & varuser & " on " & Now() & "." & _
             vbNewLine & "Delivery Note: " & varDnoteRef2
oMail.Subject = "Automatic Notification: Delivery Note"
oMail.To = "Example@Email.co.uk"
oMail.Send
Set oMail = Nothing
Set oApp = Nothing

当激活该代码的人打开Outlook时,此代码可以完美地工作.但是,当用户没有启动Outlook时,用户会在oMail.send行收到错误消息.

This code works perfectly when the person who activates the code has Outlook open. However, when the user doesn't have Outlook launched the user gets an error at the oMail.send line.

错误#287错误行0应用程序定义的错误或对象定义的错误.

error # 287 error line 0 application-defined or object-defined error.

他们得到的是桌面上的一个小图标,它是带有齿轮/嵌齿轮的Outlook符号,并显示消息another program or application is using outlook.这是我所期望的.但是为什么发送失败?

What they get is a small icon on the desktop that is the outlook symbol with a gear/cog and message saying another program or application is using outlook. Which is what I expect. But why does it fail on the send?

如何解决此错误/有解决方案吗?

How can I get around this error/ is there a solution?

编辑(更新)

Edit (Update)

更奇怪的是,当我使用F8单步执行代码时.有用!! 但是当我从表单动作中调用代码时仍然不是button_on_click

What is more strange is that when I step through my code using F8. IT WORKS!! But still not when I call the code from a form action e.g. button_on_click

推荐答案

这是我以前从未遇到过的事情,但是,我找到了解决方案.

Well this is something I have never come across before, however, I have found a solution.

错误消息本身使我想知道为什么send命令无法识别已定义的应用程序.我质疑代码是否运行得太快. (我不知道这个灯泡瞬间来自哪里,但是它解决了这个问题.)

The error message itself got me wondering why the send command wasn't recognising the defined application. I questioned whether the code was running too fast. (I don't know where this light bulb moment came from but it has fixed the issue).

我只是插入了一个循环,以强制在发送操作之前进行延迟.我这样做是因为Access无法识别application.wait.

I have simply inserted a loop to force a delay before the send operation. I did this because Access doesn't recognise application.wait.

到目前为止,测试似乎很成功!

Testing so far seems successful!

Dim T1 As Variant
Dim T2 As Variant

T1 = Now()
T2 = DateAdd("s", 1, T1)

Do Until T2 <= T1
    T1 = Now()
Loop
oMail.Send

此过程不会经常运行,一天最多可能运行5次.因此,我不必担心一秒钟的延迟会导致任何实时问题.

This procedure will not be run very often, maybe 5 times in a day max. So I am not worried about the one second delay causing any real-time issues.

这篇关于运行时错误287.在Access中使用VBA通过Outlook发送电子邮件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆