电子邮件进入收件箱后触发Outlook脚本

时间:2016-08-26 16:15:27

标签: vba ms-access outlook outlook-vba outlook-2010

我正在尝试完成访问系统与Outlook之间的集成。

系统的基础是Outlook在电子邮件进入特定收件箱时需要触发脚本。然后,此脚本打开Access DB并运行它自己的功能以浏览该收件箱,获取电子邮件中的附件并将其导入数据库。

目前,两个脚本都“工作”,因为Outlook调用Access和Access来做这件事。问题是当Outlook执行脚本时,它是在邮件实际在邮箱中之前。访问应用程序将启动,将收件箱扫描为空并在邮件实际进入收件箱之前关闭。

我已尝试在脚本中添加“暂停”循环,尝试让它等到电子邮件可读之后再打开访问应用程序,但这只会在“暂停”期间冻结Outlook而不是让电子邮件变得可读。

以下是Outlook中的脚本:

Sub ExecuteDealRequest(item As Outlook.MailItem)
    Dim currenttime As Date

    currenttime = Now
    Do Until currenttime + TimeValue("00:00:30") <= Now
    Loop

    Dim AccessApp As Access.Application
    Set AccessApp = CreateObject("Access.Application")
    AccessApp.OpenCurrentDatabase ("C:\commHU\Comm HU Request.accdb"), False
    AccessApp.Visible = True
    AccessApp.DoCmd.RunMacro "Macro1"
    Set AccessApp = Nothing
End Sub

此时:我正在使用Outlook规则来启动脚本:

Apply this rule after the message arrives
With Pricing Request in the Subject
 and on this computer only
Move it to the Pricing Requests folder
 and run Project.ExecuteDealRequest
 and stop processing more rules

任何帮助都会很棒,因为这是我工作所需的最后一件事

2 个答案:

答案 0 :(得分:3)

您不需要规则,请尝试这种方式 - 0x78

中的代码
ThisOutlookSession

答案 1 :(得分:1)

你可以尝试这样的事情,

添加此代码以等待新电子邮件

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
    ThisOutlookSession.GetNamespace("MAPI").GetItemFromID(EntryIDCollection).Subject
    ' Check for the email subject / any property
    'then call your method
End Sub
相关问题