【问题标题】:Outlook Delete email after is savedOutlook 保存后删除电子邮件
【发布时间】:2018-11-09 10:49:50
【问题描述】:

我的 VBA 技能非常有限,但到目前为止我想完成这个项目。

下面的 VBA 代码在我的 Outlook 中运行良好。它将所需的电子邮件保存到我的驱动器中。

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
    Dim olApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    Set Items = objNS.GetDefaultFolder(olFolderInbox).Items

End Sub

Private Sub Items_ItemAdd(ByVal item As Object)

On Error GoTo ErrorHandler

    'Only act if it's a MailItem
    Dim Msg As Outlook.MailItem
    If TypeName(item) = "MailItem" Then
        Set Msg = item

    'Change variables to match need. Comment or delete any part unnecessary.
        If (Msg.SenderEmailAddress = "noreply@test.com") Or _
        (Msg.Subject = "Smartsheet") Or _
        (Msg.Subject = "Defects") And _
        (Msg.Attachments.Count >= 1) Then

    'Set folder to save in.
    Dim olDestFldr As Outlook.MAPIFolder
    Dim myAttachments As Outlook.Attachments
    Dim Att As String

    'location to save in.  Can be root drive or mapped network drive.
    Const attPath As String = "C:\"


    ' save attachment
   Set myAttachments = item.Attachments
    Att = myAttachments.item(1).DisplayName
    myAttachments.item(1).SaveAsFile attPath & Att

    ' mark as read
   Msg.UnRead = False

End If
End If


ProgramExit:
  Exit Sub

ErrorHandler:
  MsgBox Err.number & " - " & Err.Description
  Resume ProgramExit
End Sub

我现在想将代码添加到保存附件后移动电子邮件到我的测试文件夹。测试文件夹在我的 Outlook 收件箱下。

我添加了 Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test")

Private Sub Application_Startup() 下,然后我将代码添加到我的 VBA 中。

代码在'标记为已读之后

If .Parent.Name = "Test" And .Parent.Parent.Name = "Inbox" Then
      ' MailItem is already in destination folder
    Else
      .Move FldrDest
    End If

没有其他变化,但它给了我编译错误。

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    MailItem.Move 实际上是一个函数,它返回已在新目的地移动的对象。 旧对象有点“丢失”,看看如何使用它(我在整个代码中注释了删除部分;)

    Set Msg = .Move(FldrDest)
    MsgBox Msg.SenderEmailAddress & vbCrLf & Msg.Subject
    

    带有一些改进建议的完整代码(参见'--> cmets):

    Private WithEvents Items As Outlook.Items
    
    'location to save in.  Can be root drive or mapped network drive.
    '-->As it is a constant you can declare it there (and so, use it in the whole module if you want to do other things with it!)
    Private Const attPath As String = "C:\"
    
    
    Private Sub Application_Startup()
        Dim olApp As Outlook.Application
        Dim objNS As Outlook.NameSpace
        Set olApp = Outlook.Application
        Set objNS = olApp.GetNamespace("MAPI")
        Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
    End Sub
    
    Private Sub Items_ItemAdd(ByVal item As Object)
    
    On Error GoTo ErrorHandler
        'Only act if it's a MailItem
        If TypeName(item) = "MailItem" Then
            Dim Msg As Outlook.MailItem
            '-->Use directly the parameter and keep it under wraps using "With", it'll improve efficiency
            With item
                'Change variables to match need. Comment or delete any part unnecessary.
                If (.SenderEmailAddress = "noreply@test.com" _
                   Or .Subject = "Smartsheet" _
                   Or .Subject = "Defects" _
                   ) _
                   And .Attachments.Count >= 1 Then
    
    
                    Dim aAtt As Outlook.Attachment
                    '-->Loop through the Attachments' collection
                    for each aAtt in item.Attachments
                        '-->You can either use aAtt.DisplayName or aAtt.FileName
                        '-->You can test aAtt.Size or aAtt.Type
    
                        'save attachment
                        aAtt.SaveAsFile attPath & aAtt.DisplayName
                    next aAtt
    
                    'mark as read
                    .UnRead = False
    
                    Dim olDestFldr As Outlook.MAPIFolder
                    Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test")
                    If .Parent.Name = "Test" And .Parent.Parent.Name = "Inbox" Then
                        'MailItem is already in destination folder
                    Else
                        Set Msg = .Move(FldrDest)
                        MsgBox Msg.SenderEmailAddress & vbCrLf & Msg.Subject
                        'Msg.delete
                    End If
                End If
            End With 'item
        End If
    
    
    ProgramExit:
      Exit Sub
    
    ErrorHandler:
      MsgBox Err.number & " - " & Err.Description
      Resume ProgramExit
    End Sub
    

    【讨论】:

    • 它给出了一个错误 - 预期:行号或标签或词干或语句结尾以及以 If ( .SenderEmailAddress = "noreply@test.com" _ to And .Attachments.Count >= 1 开头的行然后是红色的。
    • @Kalenji : 很糟糕,我忘记了_(在编辑中更改了它),我想显示在使用AndOr 时非常重要的括号。因为您发布的内容可能不会像您预期的那样表现;)
    • 像魔术一样工作。刚刚删除了不需要的弹出窗口:) 我想我需要我的 Outlook 客户端启动并运行这个宏才能工作?我的意思是一旦我注销它就不起作用了?
    • 很高兴我能帮上忙。为此,您需要启动并运行 Outlook 并登录(如果没有,它不会找到此 Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test"))。我不太确定,但我会说当您重新打开 Outlook 并发送/接收邮件时代码会运行。文档中唯一说的是This event does not run when a large number of items are added to the folder at once.
    【解决方案2】:

    比我想象的要容易。刚刚添加了一个带有Msg.Delete.的循环

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 1970-01-01
      • 2015-07-25
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      • 2020-01-19
      • 2013-11-17
      • 1970-01-01
      相关资源
      最近更新 更多