我从互联网上获取此代码,以自动将已完成的任务发送到特定文件夹(“已完成的任务”)。它不起作用,我会点击完成任务,它不会被移动。然后,我花了几个小时尝试对其进行故障排除,了解代码的每一部分是如何工作的,并且无法找到方法。 最后,我将msgboxes放在代码的三个部分中,以便查看问题所在。初始化框会弹出,但是与ItemChange对应的框不会弹出,而另一个框则不会。我的印象是没有调用ItemChange。
代码:
Public WithEvents olItems As Outlook.Items
Public Sub Application_Startup()
Set olItems = Session.GetDefaultFolder(olFolderTasks).Items
MsgBox ("initialized")
End Sub
Public Sub olItems_ItemChange(ByVal Item As Object)
Dim CompTaskf As Folder
MsgBox ("detected change")
If Item.Complete = True And Item.IsRecurring = False Then
MsgBox ("condition met")
Set CompTaskf = Session.GetDefaultFolder(olFolderTasks).Folders("Completed Tasks")
Item.Move CompTaskf
End If
End Sub
有没有人知道我是如何解决这个问题的?我试过了:
编辑:将myolItems更正回olItems,现在它可以工作但只有当我指向一个任务很少的单个文件夹时:
Set myolItems = Session.GetDefaultFolder(olFolderTasks).Folders("Inbox").Items
没有.Folders(收件箱“)
答案 0 :(得分:1)
我已经想出了如何做到这一点。我想也许问题是.Items在默认文件夹之后附加时没有正确地返回项目,所以我找到了一种方法,通过定义olItems到当前文件夹,因为每次我切换文件夹。< / p>
Public WithEvents olItems As Outlook.Items
Public WithEvents daFolder As Outlook.Explorer
Public Sub Application_Startup()
Set daFolder = Application.ActiveExplorer
'MsgBox ("initialized")
End Sub
'Sets daFolder as the active explorer window on startup, apparently necessary because i can't put Application.ActiveExplorer_FolderSwitch() as the sub
Public Sub daFolder_FolderSwitch()
Set olItems = Application.ActiveExplorer.CurrentFolder.Items
End Sub
'Every time i switch between folders, set olItems as the items of the current folder
Public Sub olItems_ItemChange(ByVal Item As Object)
Dim CompTaskf As Folder
'MsgBox ("detected change")
If TypeName(Item) = "TaskItem" And Item.Complete = True And Item.IsRecurring = False Then 'This verification that it's a task item is necessary, otherwise the code may crash
'MsgBox ("condition met")
Set CompTaskf = Session.GetDefaultFolder(olFolderTasks).Folders("Completed Tasks") 'Set folder i want to move tasks to
Item.Move CompTaskf 'Move task to the folder
End If
End Sub
答案 1 :(得分:0)
olItems变量从不初始化。您正在初始化未声明的myolItems变量。