选择来自发件人的电子邮件

Select email messages from a sender

提问人:Paolo Iseppi 提问时间:11/12/2023 最后编辑:CommunityPaolo Iseppi 更新时间:11/20/2023 访问量:82

问:

我正在尝试将来自某个发件人的所有邮件保存为主题中某些单词的 txt 文件并将其删除。

当代码命中 I get an error 时If Item.Sender.Address = "[email protected]"

对象不支持的属性或方法

Private Sub Spedizioni()
  
    Dim objNamespace As Outlook.NameSpace
    Dim sourceFolder As Outlook.MAPIFolder
    Dim Items As Outlook.Items
    Dim Item As Object
    Dim Msg As String
    Dim i As Long
    Dim sName As String
    Dim sPath As String
            
    sPath = "C:\Mail\"

    Set objNamespace = GetNamespace("MAPI")
    Set sourceFolder = objNamespace.Folders("Spareparts").Folders("Inbox")
    Set Items = sourceFolder.Items

    For i = 1 To Items.Count
        
        Set Item = Items.Item(i)
        DoEvents
        
        If Item.Sender.Address = "[email protected]" And Item.To = "[email protected]" And Left(Item.Subject, 4) = "EXPR" Then
            sName = Item.Subject
            sName = Format(Item.ReceivedTime, "yyyymmdd", vbUseSystemDayOfWeek, vbUseSystem) & Format(Item.ReceivedTime, "-hhnnss", vbUseSystemDayOfWeek, vbUseSystem) & "-" & sName & ".txt"
            Item.SaveAs sPath & sName
            Item.Delete
        End If
    Next
       
End Sub
VBA 电子邮件 Outlook

评论

1赞 Tim Williams 11/12/2023
“不工作”在帮助我们诊断您遇到的确切问题方面不是很有用......
0赞 Paolo Iseppi 11/12/2023
嗨,蒂姆,你是对的,对不起!当代码到达 If Item.Sender.Address = “[email protected]”时......我收到“对象不支持的属性或方法”消息。我将编辑正文
0赞 niton 11/12/2023
这回答了你的问题吗?将项目视为 MailItem

答:

0赞 Dmitry Streblechenko 11/12/2023 #1

首先,构造自 ,其中可以包含文件名中无效的字符,例如 等。其次,不要遍历文件夹中的所有项目 - use or .sNameMailItem.Subject":"Items.RestrictItems.Find/FindNext

评论

0赞 Paolo Iseppi 11/12/2023
嗨,德米特里,我将放置一个 replacechars 子以避免无效字符。并将查看 items.restrict 或 items.find Howeved 当我在正文中编辑时,我的主要问题是代码根本没有运行。再次感谢
0赞 Dmitry Streblechenko 11/13/2023
如果您只是创建一个没有调用它的子,它将不会运行。什么时候应该叫?
0赞 Paolo Iseppi 11/13/2023
我把这个例子 stackoverflow.com/questions/12257985/......每 5 分钟运行一次
0赞 Dmitry Streblechenko 11/13/2023
所以问题是计时器无论出于何种原因都没有启动?或者您的 OOM 代码不起作用?在前一种情况下,您确实需要先使用计时器代码。
0赞 Paolo Iseppi 11/12/2023 #2

我有一个解决方案。可能不是最优雅的(并赞赏对它的更正),但它正在工作。

Private Sub Spedizioni()
  
    Dim objNamespace As Outlook.NameSpace
    Dim sourceFolder As Outlook.MAPIFolder
    Dim Items As Outlook.Items
    Dim Item As Object
    Dim i As Long
    Dim sName As String
    Dim sPath As String
    Dim Filter As String
            
    sPath = "C:\Mail\"

    Set objNamespace = GetNamespace("MAPI")
    Set sourceFolder = objNamespace.Folders("Spareparts").Folders("Inbox")
    Set Items = sourceFolder.Items

Filter = "@SQL=""urn:schemas:httpmail:displayto"" ci_phrasematch '%[email protected]%' and ""urn:schemas:httpmail:subject"" ci_phrasematch '%Spares%'"
Set Items = Items.Restrict(Filter)


    For i = 1 To Items.Count
        
        Set Item = Items.Item(i)
        DoEvents
        
        sName = Item.Subject
        sName = Format(Item.ReceivedTime, "yyyymmdd", vbUseSystemDayOfWeek, vbUseSystem) & Format(Item.ReceivedTime, "-hhnnss", vbUseSystemDayOfWeek, vbUseSystem) & "-" & sName & ".txt"
        sName = Replace(sName, "/", "_")
        sName = Replace(sName, "\", "_")
        sName = Replace(sName, ":", "_")
        sName = Replace(sName, "?", "_")
        sName = Replace(sName, Chr(34), "_")
        sName = Replace(sName, "<", "_")
        sName = Replace(sName, ">", "_")
        sName = Replace(sName, "|", "_")
            
        Item.SaveAs sPath & sName, olTXT
        Item.Delete

    Next
       
End Sub

评论

0赞 niton 11/12/2023
考虑是强制性的。没有.Option ExplicitoMail
0赞 Paolo Iseppi 11/13/2023
谢谢 Niton,oMail 自从旧版本的代码以来就在那里,我让它注释以测试它......我已经编辑了消息并对其进行了更正以备将来使用。