将电子邮件从一个Outlook文件夹移动到另一个 [英] Move emails from one Outlook folder to another
问题描述
我正在运行此代码,将内容从Outlook文件夹TODO复制到Outlook文件夹Test.这两个文件夹都存在.
I am running this code to copy the contents from Outlook folder TODO to Outlook folder Test. Both folders exist.
我知道了
运行时错误'-2147221233(8004010f)"
"Run-time error '-2147221233 (8004010f)'
用于Set myItem = myInbox.Folders("TODO")
我尝试过
Dim myItem As Folder
Sub MoveItems()
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.Folder
Dim myDestFolder As Outlook.Folder
Dim myItems As Outlook.Items
Dim myItem As Object
Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items
Set myDestFolder = myInbox.Folders("test")
Set myItem = myInbox.Folders("TODO")
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
End Sub
推荐答案
这会将所有文件从TODO
移到Test
This will move all the Files from TODO
to Test
Sub MoveItems()
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.Folder
Dim myDestFolder As Outlook.Folder
Dim myItems As Outlook.Items
Dim myItem As Object
Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items
Set myDestFolder = myInbox.Folders("test")
Set myItems = myInbox.Folders("TODO").Items
'Debug.Print myItems.Count
For i = myItems.Count To 1 Step -1 'Iterates from the end backwards
myItems.Item(i).Move myDestFolder
Next
End Sub
您必须循环文件夹中的所有项目,该代码用于查找特定邮件.
You had to loop all the items in the folder, that code was for Finding a particular mail.
为什么我们使用向后循环(礼貌:@ComputerVersteher)
如果向前循环并删除一个项目(例如第一个),则以下项目将从其前任(例如,第二个变为第一)取代位置,并且Collection.Count
减少一个.正向循环会尝试获取直到开始Collection.Count
为止的项目,但是具有最后一个索引的项目不再可用.向后移动时,从最后一项开始,如果将其删除,则由于保留位置,因此下一项(索引-1)仍然可用.顺便说一句,For Each
循环也会产生奇怪的结果,在删除项目时应避免使用.
Reason why we used the Loop Backwards (Courtesy: @ComputerVersteher)
If you loop forward and remove an item (e.g. first one), the following items takes over position from their predecessors (e.g second-one gets first-one) andCollection.Count
is decreased by one. Forward loop would try to fetch items up to startingCollection.Count
, but the item with last index isn't available any longer. When moving backward, you start with the last item and if you remove it, the next item (index-1) is still available as it retains position. Btw,For Each
loops produce strange results too and should be avoided when deleting items.
这篇关于将电子邮件从一个Outlook文件夹移动到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!