我在這裏尋找一個起點,所以沒有代碼發佈我害怕!將附件從當前電子郵件保存到派生文件夾。
我希望(如果可能)能夠在Outlook中以正常的方式打開電子郵件(從前端開始),然後單擊按鈕運行一個宏,該宏將從此處提取附件電子郵件並將它們保存到目錄路徑(從主題派生)。
聲音可以嗎?
任何指針,鏈接代碼片斷歡迎!
我在這裏尋找一個起點,所以沒有代碼發佈我害怕!將附件從當前電子郵件保存到派生文件夾。
我希望(如果可能)能夠在Outlook中以正常的方式打開電子郵件(從前端開始),然後單擊按鈕運行一個宏,該宏將從此處提取附件電子郵件並將它們保存到目錄路徑(從主題派生)。
聲音可以嗎?
任何指針,鏈接代碼片斷歡迎!
好吧,我儘可能保存到本地文件夾並從郵件中刪除。我還沒有制定出按鈕,但我確定它不是世界上最難的東西......
所以我想看看Attachment Methods上的VBA文檔,特別是SaveAsFile
上的那個,因爲它有一個完整的例子,我用來測試的東西了。可用兩種方法是你所需要的確切的:
SaveAsFile
和
Delete
但由於VBA使任何簡單,用這兩條線,需要15人。
此外還有一個真正偉大的網站,稱爲outlookcode.com。網站管理員是一個VBA/Outlook嚮導,她會親自回答你的問題,如果他們坐在論壇上超過一天(不是保證,只是我的經驗)。該網站充滿了來源和其他人的代碼等。
這是我寫的基於MSDN的樣本,我添加了刪除方法,使它成爲一次單擊保存/刪除:
Sub getAttatchment()
Dim myInspector As Outlook.Inspector
Dim myItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
Set myInspector = Application.ActiveInspector
If Not TypeName(myInspector) = "Nothing" Then
If TypeName(myInspector.CurrentItem) = "MailItem" Then
Set myItem = myInspector.CurrentItem
Set myAttachments = myItem.Attachments
If myAttachments.Item(1).DisplayName = "" Then
Set myAttachments.Item(1).DisplayName = myAttachments.Item(1).FileName
End If
myAttachments.Item(1).SaveAsFile Environ("HOMEPATH") _
& "\My Documents\" & myAttachments.Item(1).DisplayName
myAttachments.Item(1).Delete
Else
MsgBox "The item is of the wrong type."
End If
End If
End Sub
注意,原始樣品有一個對話框,詢問用戶是否確信他們要保存,因爲它會覆蓋具有相同名稱的任何文件。我刪除它來簡化代碼。
作品 - 歡呼! 在我的(Windows XP SP3,Outlook 2007)系統上,我不得不更改代碼以包含'HOMEDIR'。 (1).SaveAsFile Environ(「HOMEDRIVE」)&「\」&Environ(「HOMEPATH」)&「\ docs \」&myAttachments.item(1).DisplayName – monojohnny 2010-02-05 14:48:53
不幸的是,這似乎創建了一個運行時在某些情況下錯誤。我相信這可能是由於附件已嵌入,或者附件中沒有文件名的情況。 – 2013-01-18 04:01:54
這很有趣,因爲我最近注意到嵌入式徽標等具有附件圖標,它激怒我無止境。我已經添加了一個可能的修復程序(尚未測試)。由於'DisplayName'屬性是可讀/寫的,'FileName'屬性是隻讀的,因此我假設'FileName'不能爲空(儘管可能不太友善),所以如果'DisplayName'是清空它默認爲'FileName'。讓我知道它是否有效。 – Anthony 2013-01-18 08:01:06
此子例程會將在用戶指定的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 style 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 attachment to the file system
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
遺憾的是不與Outlook VBA對象和事件模型來指導你足夠熟悉測試;不過,我使用Access和Excel的許多VBA,你所追求的是絕對可行的... – TonBill 2010-02-05 14:06:17