2017-02-09 38 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” –