在将新电子邮件移动到目标文件夹之前,正在旧邮件上运行脚本



我创建了一个规则,将每日电子邮件移动到特定文件夹,并运行VBA脚本来保存此电子邮件正文中的表。

收到电子邮件后,VBA会开始运行并抓取以前主题相同的电子邮件,只有在新电子邮件出现在我的目标文件夹中之后。

我试着睡觉。

有没有办法先将新电子邮件移动到目标文件夹,然后运行脚本?

Sub ExportOutlookTableToExcel()`
Dim oLookInspector As Inspector
Dim oLookMailitem As MailItem
Dim oLookWordDoc As Word.Document
Dim oLookWordTbl As Word.Table
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook 
Dim xlWrkSheet As Excel.Worksheet
'Grab Email Item
Set oLookMailitem =Application.ActiveExplorer.CurrentFolder.Items("Apples Sales")
Set oLookInspector = oLookMailitem.GetInspector
Set oLookWordDoc = oLookInspector.WordEditor

回复:我创建了一个规则,将此电子邮件移动到特定文件夹并运行VBA脚本
您不是第一个落入此陷阱的人。将移动作为代码中的最后一个操作
考虑不使用";运行脚本";规则中的代码。任何文件夹都有ItemAdd,收件箱也有NewMailEx。

Re:Set oLookMailitem=Application.ActiveExplorer.CurrentFolder.Items("Apples Sales"(
主题为"的最新邮件;苹果销售"可以这样找到:

Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant
Sub mostRecentlyReceivedMail_Subject_DemoOnly()
Dim oLookFolder As Folder
Dim oLookFolderItems As Items
Dim srchSubject As String
Dim i As Long
Dim oLookMailitem As MailItem
Set oLookFolder = ActiveExplorer.CurrentFolder
Set oLookFolderItems = oLookFolder.Items
' sort the collection not the folder
oLookFolderItems.Sort "[ReceivedTime]", True
srchSubject = "Apples Sales"
' This is demonstration code only.
' Without introducing methods to reduce the number of items to look through
'  it shows the use of an index rather than subject.
' In this case the required item is supposed to be first in the collection.
For i = 1 To oLookFolderItems.Count
' first verify object in folder is a mailitem
If oLookFolderItems(i).Class = olMail Then

' Index not subject
Set oLookMailitem = oLookFolderItems(i)

If oLookMailitem.subject = srchSubject Then
Debug.Print oLookMailitem.ReceivedTime
oLookMailitem.Display
Exit For
End If
End If

Next
End Sub

尽管主题在中有效

Set oLookMailitem =Application.ActiveExplorer.CurrentFolder.Items("Apples Sales")

它可能几乎没有实际用途。

我修改了一条规则,将此电子邮件移动到特定文件夹,并运行vba脚本从新的电子邮件正文中保存表。

无需创建规则并运行VBA脚本。相反,要立即处理传入的电子邮件,您需要处理NewMailEx事件,该事件在新邮件到达收件箱时以及客户端规则处理发生之前触发。您可以使用EntryIDCollection数组中返回的条目ID来调用NameSpace.GetItemFromID方法并处理该项。请谨慎使用此方法,以最大限度地减少对Outlook性能的影响。但是,根据客户端计算机上的设置,新邮件到达收件箱后,垃圾邮件过滤和将新邮件从收件箱移动到另一个文件夹的客户端规则等过程可能会异步进行。您不应该认为在这些事件触发后,收件箱中的项目数量总是会增加一个项目。此外,您还可以考虑在移动项目的文件夹上处理ItemAdd事件。但它有一个已知的缺点——如果同时移动超过16个项目,则不会触发事件。这是处理OOM时已知的问题。

NewMailEx事件处理程序中,您可以获得传入电子邮件的一个实例,并以编程方式将其移动到所需的文件夹中,在那里您可以运行任何其他操作。

最新更新