2017-01-09 64 views
1

我想保存附件中具有特定文件扩展名的特定发件人的附件。我在循环的If部分遇到问题。我收到运行时错误438:对象不支持此属性或方法。为什么我的Object不支持mailitem属性SenderEmailAddress?

Sub GetAttachments() 

    Dim ns As NameSpace 
    Dim folder As Outlook.MAPIFolder 
    Dim Item As Object 
    Dim Atmt As Attachment 
    Dim FileName As String 
    Dim i As Integer 
    Set ns = GetNamespace("MAPI") 
    Set Inbox = ns.GetDefaultFolder(olFolderInbox) 
    i = 0 

    If Inbox.Items.Count = 0 Then 
     MsgBox "There are no messages in the Inbox.", vbInformation, _ 
       "Nothing Found" 
     Exit Sub 
    End If 

    For Each Item In Inbox.Items 

     If Item.SenderEmailAddress = "[email protected]" Then 
      For Each Atmt In Item.Attachments 
       ' This path must exist! Change folder name as necessary. 
       If Right(Atmt.FileName, 3) = ".py" Then 
        FileName = "C:\Users\bill\Desktop\TEST\" & Atmt.FileName 
        Atmt.SaveAsFile FileName 
        i = i + 1 
       End If 
      Next Atmt 
     End If 
    Next Item 

    If i > 0 Then 
     MsgBox "I found " & i & " attached files." _ 
     & vbCrLf & "I have saved them into the C:\Users\bill\Desktop\TEST folder." _ 
     & vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!" 
    Else 
     MsgBox "I didn't find any attached files in your mail." , vbInformation, "Finished!" 
    End If 

GetAttachments_exit: 
    Set Atmt = Nothing 
    Set Item = Nothing 
    Set ns = Nothing 
    Exit Sub 

GetAttachments_err: 
    MsgBox "An unexpected error has occurred." _ 
     & vbCrLf & "Please note and report the following information." _ 
     & vbCrLf & "Macro Name: GetAttachments" _ 
     & vbCrLf & "Error Number: " & Err.Number _ 
     & vbCrLf & "Error Description: " & Err.Description _ 
     , vbCritical, "Error!" 
    Resume GetAttachments_exit 
End Sub 
+0

https://msdn.microsoft.com/en-us/library/office/ff868262.aspx如果mpfInbox.Items(ⅰ)。类= olMail然后 – niton

+0

哪'if'? 'Item.Sender'或'如果atmt'? – 0m3r

+0

如果'Item.Sender' – Chris

回答

-2

参见本实施例中

Filter = "[SenderEmailAddress] = '[email protected]'" 
    Set Items = Inbox.Items.Restrict(Filter) 

    ii = 0 

    For i = Items.Count To 1 Step -1 
     Set Item = Items.Item(i) 

     For Each Atmt In Item.Attachments 
      ' This path must exist! Change folder name as necessary. 
      If Right(Atmt.FileName, 3) = ".py" Then 
       FilePath = "C:\Temp\" 
       FileName = Atmt.FileName 
       Atmt.SaveAsFile FilePath & FileName 
       ii = ii + 1 
      End If 
     Next Atmt 
    Next 
2

的文件夹可以包含不同类型的项目。其中一些不提供SenderEmailAddress财产。尝试首先检查项目类别(或MessageCLass)。

如果您从其他应用程序自动执行Outlook,则可能会出现安全问题。见Outlook "Object Model Guard" Security Issues for Developers

不要interate该文件夹中的所有项目:

For Each Item In Inbox.Items 
    If Item.SenderEmailAddress = "[email protected]" Then 

您可以使用项目类的Find/FindNextRestrict方法,而不是。了解更多关于这些方法在下面的文章:

而且您可能会发现应用程序类有用的AdvancedSearch方法。在Outlook中使用的填写AdvancedSearch方法的主要优点是:

  • 搜索是在另一个线程执行。由于AdvancedSearch方法在后台自动运行,因此不需要手动运行其他线程。
  • 可以在任何位置搜索任何物品类型:邮件,约会,日历,便签等,即超出某个文件夹的范围。 Restrict和Find/FindNext方法可以应用于特定的Items集合(请参阅Outlook中的Folder类的Items属性)。
  • 对DASL查询的全面支持(自定义属性也可用于搜索)。您可以在MSDN的Filtering文章中阅读更多关于此的信息。为了提高搜索性能,如果为商店启用了即时搜索,则可以使用即时搜索关键字(请参阅Store类的IsInstantSearchEnabled属性)。
  • 您可以随时使用Search类的Stop方法停止搜索过程。

查看Advanced search in Outlook programmatically: C#, VB.NET了解更多信息。

+0

我删除了“Item”。从“Item.SenderEmailAddress”。如下所示:'对于Inbox.Items中的每个项目 如果SenderEmailAddress =“[email protected]”Then'并且删除了错误消息,但没有找到满足该条件的任何内容。 – Chris

+1

没有必要删除'项目'部分。如果没有物体,物业就没有任何意义。 –

相关问题