Outlook仅在特定时间框架内检查电子邮件 [英] Outlook checks e-mails only in specific time frame

查看:253
本文介绍了Outlook仅在特定时间框架内检查电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个VBA outlook宏,用于检查特定时间段内文件夹中的项目。
目前,我的代码通过指定文件夹中的所有邮件,但这不是一个选项,因为该文件夹有数千个邮件,所以宏永远运行,任何想法,如何获得脚本仅从例如:从3/16/2015 12:00 PM到3/16/2015 2:00 PM检查邮件,不检查那个时间段内的任何电子邮件?

I need a VBA outlook macro that checks items in a folder in a specific time frame. At the moment my code goes through all the mails in the specified folder, but this is not an option since the folder has thousands of mails, so it takes forever for the macro to run, any ideas, how to get the script to check the mails only from for example: from 3/16/2015 12:00PM to 3/16/2015 2:00PM and not check for any e-mails out of that time frame?

这是我现在所在的:

Sub ExportToExcel()



Dim appExcel As Excel.Application
Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet
Dim rng As Excel.Range
Dim workbookFile As String
Dim msg As Outlook.MailItem
Dim nms As Outlook.NameSpace
Dim fld As Outlook.MAPIFolder
Dim itm As Object

 'Folder path and file name of an existing Excel workbook

workbookFile = "C:\Users\OutlookItems.xls"

 'Select export folder
Set nms = Application.GetNamespace("MAPI")
Set fld = nms.PickFolder

 'Handle potential errors with Select Folder dialog box.
If fld Is Nothing Then
    MsgBox "There are no mail messages to export", vbOKOnly, _
    "Error"
    Exit Sub
ElseIf fld.DefaultItemType <> olMailItem Then
    MsgBox "There are no mail messages to export", vbOKOnly, _
    "Error"
    Exit Sub
ElseIf fld.Items.Count = 0 Then
    MsgBox "There are no mail messages to export", vbOKOnly, _
    "Error"
    Exit Sub
End If

' Open and activate Excel workbook.
Set appExcel = CreateObject("Excel.Application")
Set wkb = appExcel.Workbooks.Open(workbookFile)
Set wks = wkb.Sheets(1)
wks.Activate
appExcel.Application.Visible = True
Set rng = wks.Range("A1")

 'Copy field items in mail folder.

For Each itm In fld.Items
    If itm.Class = Outlook.OlObjectClass.olMail Then
        Set msg = itm
        If InStr(msg.Subject, "Error in WU_Send") > 0 And DateDiff("h", msg.SentOn, Now) <= 2 Then

            rng.Offset(0, 4).Value = msg.Body
            Set rng = rng.Offset(1, 0)
        End If
        End If
    Next     
End Sub

,问题在于这个特定的部分:

and the problem lies in this particular part:

    For Each itm In fld.Items
    If itm.Class = Outlook.OlObjectClass.olMail Then
        Set msg = itm
        If InStr(msg.Subject, "Error in WU_Send") > 0 And DateDiff("h", msg.SentOn, Now) <= 2 Then

如何告诉代码仅在指定时间之间查看电子邮件,并忽略其余的?

谢谢您或您的回复,意见和建议提前!

Thank you or your replies, comments and suggestions in advance!

推荐答案

您需要使用查找 / FindNext 限制项目类的方法,而不是迭代文件夹中的所有项目。例如:

You need to use the Find/FindNext or Restrict methods of the Items class instead of iterating through all items in the folder. For example:

Sub DemoFindNext() 
 Dim myNameSpace As Outlook.NameSpace 
 Dim tdystart As Date 
 Dim tdyend As Date 
 Dim myAppointments As Outlook.Items 
 Dim currentAppointment As Outlook.AppointmentItem 

 Set myNameSpace = Application.GetNamespace("MAPI") 
 tdystart = VBA.Format(Now, "Short Date") 
 tdyend = VBA.Format(Now + 1, "Short Date") 
 Set myAppointments = myNameSpace.GetDefaultFolder(olFolderCalendar).Items 
 Set currentAppointment = myAppointments.Find("[Start] >= """ & tdystart & """ and [Start] <= """ & tdyend & """") 
 While TypeName(currentAppointment) <> "Nothing" 
   MsgBox currentAppointment.Subject 
   Set currentAppointment = myAppointments.FindNext 
 Wend 
End Sub

有关详细信息和示例代码,请参阅以下文章:

See the following articles for more information and sample code:

  • How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
  • How To: Use Restrict method to retrieve Outlook mail items from a folder

此外,您可能会发现 AdvancedSearch 应用程序类的方法有帮助。使用AdvancedSearch方法的主要优点如下所示:

Also you may find the AdvancedSearch method of the Application class helpful. The key benefits of using the AdvancedSearch method are listed below:


  • 搜索在另一个线程中执行。您不需要手动运行另一个线程,因为AdvancedSearch方法会在后台自动运行。

  • 可以搜索任何项目类型:邮件,约会,日历,笔记等任何位置,即超出某个文件夹的范围。限制和查找/ FindNext方法可以应用于特定的Items集合(请参阅Outlook中的Folder类的Items属性)。

  • 完全支持DASL查询(可以使用自定义属性用于搜索)。您可以在MSDN中的过滤文章中阅读更多信息。为了提高搜索性能,如果为商店启用即时搜索,可以使用即时搜索关键字(请参阅商店类的IsInstantSearchEnabled属性)。

  • 您可以停止搜索过程使用Search类的Stop方法。

  • The search is performed in another thread. You don’t need to run another thread manually since the AdvancedSearch method runs it automatically in the background.
  • Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The Restrict and Find/FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook).
  • Full support for DASL queries (custom properties can be used for searching too). You can read more about this in the Filtering article in MSDN. To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).
  • You can stop the search process at any moment using the Stop method of the Search class.

这篇关于Outlook仅在特定时间框架内检查电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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