【问题标题】:Download attachments from specific folder in Outlook从 Outlook 中的特定文件夹下载附件
【发布时间】:2016-06-29 21:33:46
【问题描述】:

我对 vba 的熟悉程度不足以根据我的需要对其进行修改。

我需要从特定文件夹下载附件。

我找到了这个example,但我不确定如何获取这些电子邮件发送到的文件夹。

我有一个规则,当这些电子邮件进来时,它会将它们放在不同的文件夹中。

我想在此处运行宏,因此它只会从这些电子邮件中删除附件并将它们放在本地计算机文件夹中。

我需要更改哪些部分才能使其满足我的需求?

Public Sub SaveAttachments(Item As Outlook.MailItem)

If Item.Attachments.Count > 0 Then

Dim objAttachments As Outlook.Attachments
Dim lngCount As Long
Dim strFile As String
Dim sFileType As String
Dim i As Long

Set objAttachments = Item.Attachments
    lngCount = objAttachments.Count
 For i = lngCount To 1 Step -1

' Get the file name.
 strFile = objAttachments.Item(i).FileName

 ' Get the path to your My Documents folder
    strfolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
    strfolderpath = strfolderpath & "\Attachments\"

' Combine with the path to the folder.
 strFile = strfolderpath & strFile

' Save the attachment as a file.
 objAttachments.Item(i).SaveAsFile strFile

 Next i
End If

End Sub

【问题讨论】:

  • 您对文件夹了解多少?它的条目ID?文件夹路径?
  • 我只是不确定如何在 vba 中写入文件夹路径。它只是收件箱旁边电子邮件中的另一个文件夹。
  • 如果链接页面发生更改,仅链接通常会变得无效 - 我已经为您的问题添加了上下文,因此您的其他用户会知道您的要求,还有@DmitryStreblechenko 很好的答案,我也有添加第二个答案。 :-)

标签: vba outlook outlook-2010


【解决方案1】:

代码位于 ThisOutlookSession 更新文件夹名称 "Temp"

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
    Dim olNameSpace As Outlook.NameSpace
    Dim olFolder  As Outlook.MAPIFolder

    Set olNameSpace = Application.GetNamespace("MAPI")
    Set olFolder = olNameSpace.GetDefaultFolder(olFolderInbox).Folders("TEMP")
    Set Items = olFolder.Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)
    If TypeOf Item Is Outlook.MailItem Then
        SaveAttachments Item
    End If
End Sub

'// http://www.slipstick.com/developer/save-attachments-to-the-hard-drive/
Public Sub SaveAttachments(Item As Outlook.MailItem)

    If Item.Attachments.Count > 0 Then

        Dim objAttachments As Outlook.Attachments
        Dim lngCount As Long
        Dim strFile As String
        Dim sFileType As String
        Dim i As Long

        Set objAttachments = Item.Attachments
            lngCount = objAttachments.Count
        For i = lngCount To 1 Step -1

            ' Get the file name.
            strFile = objAttachments.Item(i).FileName

            ' Get the path to your My Documents folder
            strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
            strFolderpath = strFolderpath & "\Attachments\"

            ' Combine with the path to the folder.
            strFile = strFolderpath & strFile

            ' Save the attachment as a file.
            objAttachments.Item(i).SaveAsFile strFile

        Next i
    End If

End Sub

【讨论】:

    【解决方案2】:

    要打开与收件箱同级的文件夹,请打开收件箱,然后上一级至其父级,然后按名称检索您的文件夹:

    set MyFolder = Application.Session.GetDefaultFolder(olFolderInbox).Parent.Folders.Item("My Folder Name")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      • 2015-11-09
      • 2021-01-01
      • 2015-09-19
      相关资源
      最近更新 更多