2017-02-09 20 views
1

我已經在各種網站上看到了一些處理此問題的帖子,但尚未得到它的工作。也許我很密集,不知道,但這裏有: 事件似乎並不想開火。在ThisOutlookSession 在頂部潛艇前/功能中 代碼:VBA Outook 2010 - 編碼ItemAdd事件

'Declare event handler 
Public WithEvents myOutlookItems As Outlook.Items 
Private Sub Application_Startup() 
    Set myOutlookItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Items 
End Sub 
Public Sub myOutlookItems_ItemAdd(ByVal Item As Object) 
    If TypeName(Item) = MailItem Then 
     MsgBox ("Got a message") 
    End If 
End Sub' 

感謝您的幫助!

+0

看一看宏安全。默認情況下,未簽名的宏被禁用 – SBF

+0

謝謝,但這是非常開放的,我有其他的宏正在觸發(即application_itemsend) –

+0

像@DmitryStreblechenko說使用'olMail'或''MailItem「'它應該工作。 – 0m3r

回答

1

類型名返回一個字符串,那麼你的代碼應該是

If TypeName(Item) = "MailItem" Then 

或者你也可以檢查類屬性的值(所有OOM對象公開吧):

If Item.Class = olMail Then ' olMail== 43 
+1

謝謝@DmitryStreblechenko!我已經改變它:「如果TypeName(Item)= MailItem Then」但是我更喜歡你的方法,所以將它改爲「如果Item.Class = olMail」 –