2010-10-21 103 views
0

假設我有我的郵箱配置,我有一個特殊的文件夾用於Outlook 2007中的附件郵件。我想要做的是 我。或者將Outlook配置文件保存到指定文件夾(郵件附件)中的郵件附件到我的計算機驅動器中的特定文件夾中的所需文件夾中在Outlook郵箱郵件中存儲附件的最快捷方式是什麼?

ii。或者,如果我可以寫一些宏或腳本將這些全部複製到我的電腦位置。如果可以,請給我快速的概述或向我介紹一些地方。

回答

1

下面的代碼會自動將附件保存到目錄。使用Outlook規則自動在每個傳入郵件上運行此宏。

Sub AutoSaveAttachment(Item As Outlook.MailItem) 
    Dim olAtt As Attachment 
    Dim i As Integer 
    Dim FIleNamewithDate As String 
    Const FILE_PATH As String = "C:\" 

    If Item.Attachments.Count > 0 Then 
     For i = 1 To Item.Attachments.Count 
      Set olAtt = Item.Attachments(i) 
      olAtt.SaveAsFile FILE_PATH & olAtt.FileName 


     Next i 
    End If 

    Set olAtt = Nothing 

End Sub 
1

此子例程會將在用戶指定的Outlook文件夾中找到的所有附件保存到文件系統上的用戶指定目錄。它還通過指向清除文件的鏈接更新每條消息。

它還包含額外的註釋,以幫助突出顯示.Delete方法如何動態縮小附件容器(在註釋中搜索「~~」)。

該宏僅在Outlook 2010中

' ------------------------------------------------------------ 
' Requires the following references: 
' 
' Visual Basic for Applications 
' Microsoft Outlook 14.0 Object Library 
' OLE Automation 
' Microsoft Office 14.0 Object Library 
' Microsoft Shell Controls and Automation 
' ------------------------------------------------------------ 

Public Sub SaveOLFolderAttachments() 

    ' Ask the user to select a file system folder for saving the attachments 
    Dim oShell As Object 
    Set oShell = CreateObject("Shell.Application") 
    Dim fsSaveFolder As Object 
    Set fsSaveFolder = oShell.BrowseForFolder(0, "Please Select a Save Folder:", 1) 
    If fsSaveFolder Is Nothing Then Exit Sub 
    ' Note: BrowseForFolder doesn't add a trailing slash 

    ' Ask the user to select an Outlook folder to process 
    Dim olPurgeFolder As Outlook.MAPIFolder 
    Set olPurgeFolder = Outlook.GetNamespace("MAPI").PickFolder 
    If olPurgeFolder Is Nothing Then Exit Sub 

    ' Iteration variables 
    Dim msg As Outlook.MailItem 
    Dim att As Outlook.attachment 
    Dim sSavePathFS As String 
    Dim sDelAtts As String 

    For Each msg In olPurgeFolder.Items 

    sDelAtts = "" 

    ' We check each msg for attachments as opposed to using .Restrict("[Attachment] > 0") 
    ' on our olPurgeFolder.Items collection. The collection returned by the Restrict method 
    ' will be dynamically updated each time we remove an attachment. Each update will 
    ' reindex the collection. As a result, it does not provide a reliable means for iteration. 
    ' This is why the For Each loops will not work. 
    If msg.Attachments.Count > 0 Then 

     ' This While loop is controlled via the .Delete method 
     ' which will decrement msg.Attachments.Count by one each time. 
     While msg.Attachments.Count > 0 

     ' Save the file 
     sSavePathFS = fsSaveFolder.Self.Path & "\" & msg.Attachments(1).FileName 
     msg.Attachments(1).SaveAsFile sSavePathFS 

     ' Build up a string to denote the file system save path(s) 
     ' Format the string according to the msg.BodyFormat. 
     If msg.BodyFormat <> olFormatHTML Then 
      sDelAtts = sDelAtts & vbCrLf & "<file://" & sSavePathFS & ">" 
     Else 
      sDelAtts = sDelAtts & "<br>" & "<a href='file://" & sSavePathFS & "'>" & sSavePathFS & "</a>" 
     End If 

     ' Delete the current attachment. We use a "1" here instead of an "i" 
     ' because the .Delete method will shrink the size of the msg.Attachments 
     ' collection for us. Use some well placed Debug.Print statements to see 
     ' the behavior. 
     msg.Attachments(1).Delete 

     Wend 

     ' Modify the body of the msg to show the file system location of 
     ' the deleted attachments. 
     If msg.BodyFormat <> olFormatHTML Then 
     msg.Body = msg.Body & vbCrLf & vbCrLf & "Attachments Deleted: " & Date & " " & Time & vbCrLf & vbCrLf & "Saved To: " & vbCrLf & sDelAtts 
     Else 
     msg.HTMLBody = msg.HTMLBody & "<p></p><p>" & "Attachments Deleted: " & Date & " " & Time & vbCrLf & vbCrLf & "Saved To: " & vbCrLf & sDelAtts & "</p>" 
     End If 

     ' Save the edits to the msg. If you forget this line, the attachments will not be deleted. 
     msg.Save 

    End If 

    Next 

End Sub 
測試
相關問題