我創建這個宏,讓我做到以下幾點:刪除電子郵件附件在Outlook
- 選擇一個文件夾,以附件保存到
- 選擇一個日期範圍從 下載電子郵件附件
保存電子郵件後,我需要從電子郵件中刪除保存的附件,並將其替換爲保存位置的鏈接。
這裏是我使用的代碼:
Option Explicit
Sub SaveMailAttachments()
On Error Resume Next
Dim ns As NameSpace
Set ns = GetNamespace("MAPI")
Dim Inbox As MAPIFolder
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Dim saveFolder As String
Dim subFolder As MAPIFolder
Dim Item As Object
Dim Attach As Attachment
Dim FileName As String, fName As String
Dim i As Integer
Dim Searchdate As String
Dim SentDate As String
Dim sntDate As Date
Searchdate = InputBox("Please enter a Previous date to search from")
saveFolder = BrowseForFolder("Select the folder you will like to save the attachments to.")
If saveFolder = vbNullString Then Exit Sub
i = 0
If Inbox.Items.Count = 0 Then
MsgBox "There are no messages in the inbox.", vbInformation, _
"nothing Found"
Exit Sub
End If
On Error Resume Next
For Each Item In Inbox.Items
sntDate = Item.SentOn
SentDate = Format(sntDate, "mm/dd/yyyy")
For Each Attach In Item.Attachments
If Searchdate < SentDate Then
FileName = saveFolder & "\" & Attach.FileName
Attach.SaveAsFile FileName
i = i + 1
End If
Next Attach
'End If
Next Item
End Sub
Dmitry Streblechenko無法幫助您的一個原因是您的代碼頂部的「On Error Resume Next」。只有在有特定目的時才使用它,而不是繞過所有錯誤。它應該緊跟着「On Error GoTo 0」。特別是在調試過程中,您需要查看錯誤。 – niton 2014-11-07 16:08:49
我現在試過了,它給了我一個錯誤,但它是完全不同的東西。因爲我試圖編輯整個msg來添加被刪除文件的名稱。一旦我刪除相同的問題不斷髮生。 @niton – Dre4821 2014-11-07 16:45:01