【问题标题】:Save Attachment on arriving email保存到达电子邮件的附件
【发布时间】:2019-05-07 14:33:02
【问题描述】:

我创建了一个 Outlook 规则来保存附件,然后将其移至“已删除邮件”文件夹。当我在收件箱中突出显示到达的电子邮件然后将电子邮件移动到已删除邮件文件夹时,该代码有效。

当新电子邮件到达时,它会将来自不同电子邮件的附件保存在收件箱中,而不是将电子邮件移动到已删除邮件文件夹中。

Outlook 规则是:

Apply this rule after the message arrives
from Sender
 and with Gift Card in the subject
 and on this computer only
run Project1.SaveAttachments
Public Sub SaveAttachments(MItem As Outlook.Mailitem)
    Dim objOL As Outlook.Application
    Dim objMsg As Outlook.Mailitem 'Object
    Dim objAttachments As Outlook.Attachments
    Dim objSelection As Outlook.Selection
    Dim i As Long
    Dim lngCount As Long
    Dim strFile As String
    Dim strFolderpath As String
    Dim objNamespace As Outlook.NameSpace
    Dim objDestFolder As Outlook.MAPIFolder
    
On Error Resume Next

Set objOL = CreateObject("Outlook.Application")

Set objSelection = objOL.ActiveExplorer.Selection

strFolderpath = "Y:\"

For Each objMsg In objSelection

    Set objAttachments = objMsg.Attachments
    lngCount = objAttachments.Count
    strDeletedFiles = ""

    If lngCount > 0 Then

        For i = lngCount To 1 Step -1

            strFile = objAttachments.Item(i).FileName
            strFile = strFolderpath & strFile
            objAttachments.Item(i).SaveAsFile strFile
        Next i
        Set objNamespace = objOL.GetNamespace("MAPI")
        Set objDestFolder = objNamespace.GetDefaultFolder(olFolderDeletedItems)
                    
        objMsg.Move objDestFolder

    End If
    
Next
 
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
Set objNamespace = Nothing
Set objDestFolder = Nothing

End Sub

【问题讨论】:

  • 我使用了您提供的链接中的一些代码,并添加了更多代码以将电子邮件移至已删除项目。当我通过突出显示它手动运行它时它可以工作,但当新电子邮件到达时它不会工作。它将来自不同电子邮件的附件保存在收件箱文件夹中,并且不会将刚到达的电子邮件移动到已删除邮件文件夹中。
  • 问题的答案是删除与选择相关的代码。
  • 你在 MItem 中有相关的对象。不要在选择中寻找它。
  • 如果项目总是有附件然后添加到你的规则然后清理你的代码 - 保持简单

标签: vba outlook outlook-2010


【解决方案1】:

根据我的测试,您可以使用以下代码保存电子邮件附件并删除它:

Sub SaveAutoAttach()

Dim object_attachment As Outlook.attachment

Dim saveFolder As String
Dim oOutlook As Object
Dim oOlns As Object
Dim oOlInb As Object
Dim unRead, m As Object, att As Object
Dim some As String, other As String

Const olFolderInbox = 6

'~~> Get Outlook instance
Set oOutlook = GetObject(, "Outlook.application")
Set oOlns = oOutlook.GetNamespace("MAPI")
Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)

'~~> Check if there are any actual unread emails
Set unRead = oOlInb.Items.Restrict("[UnRead] = True")

If unRead.Count = 0 Then
    MsgBox "NO Unread Email In Inbox"
Else

    some = ""
    other = ""
    saveFolder = "D:\"
    For Each m In unRead
        If m.Attachments.Count > 0 Then
            For Each object_attachment In m.Attachments
            ' Criteria to save .doc files only
                If InStr(object_attachment.DisplayName, ".doc") Then
                    object_attachment.SaveAsFile saveFolder & "\" & object_attachment.DisplayName
                End If
             Next
        End If
        m.Delete
    Next m
End Sub

更多信息,请参考此链接:

Auto Download Outlook Email Attachment – Code in VBA by Topbullets.com

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 2021-06-26
    • 2012-01-15
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多