2017-03-02 73 views
1

我在Outlook框中的VBA中有一个侦听器,用于在从特定电子邮件接收邮件时执行操作。检查发件人电子邮件地址

问题是,如果我收到错误邮件(未发送邮件),那么我的条件运行在没有该属性的邮件上,所以我的方法崩溃。

我不知道这个问题可能是什么。

有没有人有想法,如果我可以测试该属性是否存在或者是否有另一个属性,我可以检查以确定我的发件人是否匹配?

提前感谢

Sub SetFlagIcon() 

Dim mpfInbox As Outlook.Folder 

Dim obj As Outlook.MailItem 

Dim i As Integer 



Set mpfInbox = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("Test") 

' Loop all items in the Inbox\Test Folder 

For i = 1 To mpfInbox.Items.Count 

If mpfInbox.Items(i).Class = olMail Then 

Set obj = mpfInbox.Items.Item(i) 

If obj.SenderEmailAddress = "[email protected]" Then 

'Set the yellow flag icon 

obj.FlagIcon = olYellowFlagIcon 

obj.Save 

End If 

End If 

Next 

End Sub 

回答

3

Dim obj as a generic Object - 有比MailItem在收件箱其他对象,也提高了你的循环尝试使用Items.Restrict Method (Outlook)

Option Explicit 
Sub SetFlagIcon() 
    Dim mpfInbox As Outlook.Folder 
    Dim obj As Object 
    Dim Items As Outlook.Items 
    Dim i As Long 
    Dim Filter As String 

    Set mpfInbox = Application.GetNamespace("MAPI").GetDefaultFolder _ 
            (olFolderInbox).Folders("Temp") 

    Filter = "[SenderEmailAddress] = '[email protected]'" 

    Set Items = mpfInbox.Items.Restrict(Filter) 

    ' Loop all items in the Inbox\Test Folder 
    For i = 1 To Items.Count 
     If Items(i).Class = olMail Then 
      Set obj = Items(i) 
      'Set the yellow flag icon 
      obj.FlagIcon = olYellowFlagIcon 
      obj.Save 
     End If 
    Next 

End Sub 

Items.Restrict Method应用滤镜的Items集合,返回包含所有项目从匹配过滤,原来的一个新的集合。

相关问题