根据 ReceivedTime 将项目移动到 Outlook 中的指定子文件夹 [英] Move items to a specified subfolder inside Outlook based on ReceivedTime
问题描述
我正在尝试移动 Outlook 项目,但是代码运行时没有错误消息,但没有移动电子邮件.
这让我相信必要的 IF 条件
永远不会被满足?但是我可能是错的.
请在下面找到代码.
Sub Gatekeeper()Dim aItem 作为对象将邮件调暗为对象Dim strTime As String将项目调暗为 Outlook.ItemsDim olNs 作为 Outlook.NameSpaceDim 子文件夹作为 Outlook.MAPIFolderSet olNs = Application.GetNamespace("MAPI")设置邮件 = olNs.GetDefaultFolder(olFolderInbox)设置项目 = mail.Items对于 Items 中的每个 aItemstrTime = aItem.ReceivedTime如果 strTime >#6:00:00 PM# 和 strTime <#5:30:00 AM# 那么设置子文件夹 = mail.Folders("Nights")aItem.Move 子文件夹万一下一个项目结束子
在移动/删除或修改集合项时,不应使用 For Each...Next Loop
<块引用>当您想为集合或数组的每个元素重复一组语句时,请使用 For Each...Next 循环.
<小时>使用 For...Next 语句 - 向下 for 循环:
对于 i = Items.Count 到 1 步 -1下一个
<块引用>
A 对于...Next 语句 当您可以将循环的每次迭代与控制变量相关联并确定该变量的初始值和最终值时,效果会很好.但是,当您处理一个集合时,初始值和最终值的概念没有意义,您也不一定知道该集合有多少个元素.在这种情况下,For Each...Next loop 通常是更好的选择.
<小时>还请记住,除了对象rel="nofollow noreferrer">MailItem 在您的收件箱中,因此请检查 If Items.Class = olMail Then 否则你会在循环中遇到错误
您可能还想使用 Items.Restrict 方法 (Outlook) 改进您的循环
<块引用>Items.Restrict Method 将过滤器应用于 Items 集合,返回一个新集合,其中包含原始中与过滤器匹配的所有项目.
该方法是使用查找方法 或 FindNext 方法 迭代集合中的特定项目.如果项目数量较少,Find 或 FindNext 方法 比过滤更快.如果集合中有大量项目,Restrict 方法会明显更快,尤其是当预计只能找到大型集合中的少数项目时._
代码示例
选项显式公共子示例()Dim olNs 作为 Outlook.NameSpace将收件箱调暗为 Outlook.MAPIFolder将项目调暗为 Outlook.Items将项目变暗为对象将过滤器调暗为字符串昏暗的我过滤器 = "[ReceivedTime] >='" &_CStr(日期 - 1) &_" 06:00PM' AND [ReceivedTime] <'" &_CStr(日期) &05:30AM'"调试.打印过滤器Set olNs = Application.GetNamespace("MAPI")设置收件箱 = olNs.GetDefaultFolder(olFolderInbox)设置项目 = Inbox.Items.Restrict(Filter)Items.Sort "[ReceivedTime]"对于 i = Items.Count To 1 Step -1事件如果 TypeOf Items(i) 是 MailItem 那么Debug.Print Items(i) ' 在立即窗口上打印 (Ctrl+G)Set Item = Items(i)Item.Move Inbox.Folders("Nights")万一下一个结束子
确保正确设置过滤器,我假设您正在查看昨天的 06:00PM
CStr(日期 - 1) =(今天 - 1 天)
Date 类型始终包含日期和时间信息.出于类型转换的目的,Visual Basic 将 1/1/0001(第一年的 1 月 1 日)视为日期的中性值,将 00:00:00(午夜)视为时间的中性值.CStr 在结果字符串中不包含中性值.例如,如果将#January 1, 0001 9:30:00# 转换为字符串,则结果为9:30:00 AM";日期信息被抑制.但是,日期信息仍然存在于原始日期值中,可以使用日期部分.
<小时>I'm trying to move Outlook Items, However the code runs with no error messages but no emails are moved.
This leads me to belief the necessary IF condition
is never being met? However I could be wrong.
Please find code below.
Sub Gatekeeper()
Dim aItem As Object
Dim mail As Object
Dim strTime As String
Dim Items As Outlook.Items
Dim olNs As Outlook.NameSpace
Dim subfolder As Outlook.MAPIFolder
Set olNs = Application.GetNamespace("MAPI")
Set mail = olNs.GetDefaultFolder(olFolderInbox)
Set Items = mail.Items
For Each aItem In Items
strTime = aItem.ReceivedTime
If strTime > #6:00:00 PM# And strTime < #5:30:00 AM# Then
Set subfolder = mail.Folders("Nights")
aItem.Move subfolder
End If
Next aItem
End Sub
You shouldn't use For Each...Next Loop when you are Moving /deleting or modifying collection Items
Use a For Each...Next loop when you want to repeat a set of statements for each element of a collection or array.
Work with For...Next Statement - Down for loop:
For i = Items.Count to 1 step -1
Next
A For...Next Statement works well when you can associate each iteration of a loop with a control variable and determine that variable's initial and final values. However, when you are dealing with a collection, the concept of initial and final values isn't meaningful, and you don't necessarily know how many elements the collection has. In this kind of case, a For Each...Next loop is often a better choice.
Also remember there are objects other than MailItem in your Inbox so check If Items.Class = olMail Then or you will encounter and error on your loop
You may also wanna use Items.Restrict Method (Outlook) to improve your loop
Items.Restrict Method Applies a filter to the Items collection, returning a new collection containing all of the items from the original that match the filter.
The method is an alternative to using the Find method or FindNext method to iterate over specific items within a collection. The Find or FindNext methods are faster than filtering if there are a small number of items. The Restrict method is significantly faster if there is a large number of items in the collection, especially if only a few items in a large collection are expected to be found. _
Code Example
Option Explicit
Public Sub Example()
Dim olNs As Outlook.NameSpace
Dim Inbox As Outlook.MAPIFolder
Dim Items As Outlook.Items
Dim Item As Object
Dim Filter As String
Dim i As Long
Filter = "[ReceivedTime] >= '" & _
CStr(Date - 1) & _
" 06:00PM' AND [ReceivedTime] < '" & _
CStr(Date) & " 05:30AM'"
Debug.Print Filter
Set olNs = Application.GetNamespace("MAPI")
Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
Set Items = Inbox.Items.Restrict(Filter)
Items.Sort "[ReceivedTime]"
For i = Items.Count To 1 Step -1
DoEvents
If TypeOf Items(i) Is MailItem Then
Debug.Print Items(i) ' Print on Immediate Window (Ctrl+G)
Set Item = Items(i)
Item.Move Inbox.Folders("Nights")
End If
Next
End Sub
Make sure to set your Filter correctly, I'm assuming your looking at yesterdays 06:00PM
CStr(Date - 1) = (today - 1 day)
The Date type always contains both date and time information. For purposes of type conversion, Visual Basic considers 1/1/0001 (January 1 of the year 1) to be a neutral value for the date, and 00:00:00 (midnight) to be a neutral value for the time. CStr does not include neutral values in the resulting string. For example, if you convert #January 1, 0001 9:30:00# to a string, the result is "9:30:00 AM"; the date information is suppressed. However, the date information is still present in the original Date value and can be recovered with functions such as DatePart.
这篇关于根据 ReceivedTime 将项目移动到 Outlook 中的指定子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!