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

查看:93
本文介绍了运行时错误 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.

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

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

他们得到的是桌面上的一个小图标,它是带有齿轮/齿轮和消息的 Outlook 符号,说明另一个程序或应用程序正在使用 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?

编辑(更新)

更奇怪的是,当我使用 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.

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

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天全站免登陆