我在Outlook VBA編程中遇到了一些小問題,並且想知道是否有解決方案,或者這只是另一個「已知問題」。只有當選擇了文件夾時,IMAP文件夾上的Outlook ItemAdd事件纔會觸發
語境:
我已經配置了Outlook電子郵件帳戶通過IMAP訪問我的網站的電子郵件服務提供商。在Outlook中,我可以正常看到我的Web電子郵件文件夾。我的提供商的垃圾郵件過濾器將垃圾郵件移入垃圾郵件文件夾。
我想自動將放入垃圾郵件文件夾的郵件移動到我本地pst文件中的另一個文件夾中。
我讓它工作了99%(通過下面提供的代碼以供參考)。
問題:
我可以看到,有在垃圾郵件文件夾中的郵件(有文件夾名稱旁邊有一個大膽的未讀郵件數),但ItemAdd甚至只會火當我點擊在文件夾上。到那時,我看到了垃圾郵件文件夾的內容,然後看到所有新的垃圾郵件都被移到了我的本地文件夾中。
除ItemAdd外還有另一個觸發源嗎?我可以用它來運行我的代碼而無需點擊文件夾?當文件夾的未讀計數發生變化時是否觸發事件?
技術細節:
- 的Windows 8操作系統
- 使用Outlook 2002(是的,我知道...)
- 我是一個經驗豐富的C/C++開發者,但VBA最少的經驗,而沒有Outlook。
VBA代碼:
Public WithEvents myItems As Outlook.Items
Public Sub Application_Startup()
Dim myNameSpace As Outlook.NameSpace
Const mailboxName As String = "Mail.com"
Const subfolderName As String = "Spam"
' Reference the items in the MAPI spam folder
' Because myOlItems is declared "WithEvents" the ItemAdd event will fire below.
Set myNameSpace = Application.GetNamespace("MAPI")
On Error GoTo noSpamFolder
Set myItems = myNameSpace.Folders(mailboxName).Folders(subfolderName).Items
On Error GoTo 0
Exit Sub
noSpamFolder:
MsgBox "Unable to find folder <" & mailboxName & "/" & subfolderName & ">"
End Sub
Private Sub myItems_ItemAdd(ByVal Item As Object)
Dim suspectFolder As Outlook.MAPIFolder
' Check to make sure it is an Outlook mail message, otherwise
' subsequent code will probably fail depending on what type
' of item it is.
If TypeName(Item) = "MailItem" Then
' Move message to the 'suspect' folder
On Error GoTo noSuspectFolder
Set suspectFolder = Outlook.Session.GetDefaultFolder(olFolderInbox).Folders("suspect")
On Error GoTo 0
Item.Move suspectFolder
End If
Exit Sub
noSuspectFolder:
MsgBox "Unable to find folder <suspect> as a sub-folder of default inbox folder"
End Sub
您是否已經獲得腳本工作?我試圖找到一種事件的方式來創建一個日曆事件時,我想這將沿着'Outlook.MAPIFolder.itemsAdd'的行,但我不完全確定。我基本上沒有使用VBscript的經驗,也許你可以指點我正確的方向... – Silentdarkness 2013-09-06 06:12:06
從我的初始描述沒有改善,這意味着我仍然需要點擊IMAP文件夾來處理它的項目。我可以忍受這一點。 – 2013-09-07 10:26:22