2017-03-02 49 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集合,返回包含所有項目從匹配過濾,原來的一個新的集合。

相關問題