Public
Sub
SaveAttachments()
Dim
objOL
As
Outlook.Application
Dim
objMsg
As
Outlook.MailItem
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
strDeletedFiles
As
String
strFolderpath = CreateObject(
"WScript.Shell"
).SpecialFolders(16)
On
Error
Resume
Next
Set
objOL = CreateObject(
"Outlook.Application"
)
Set
objSelection = objOL.ActiveExplorer.Selection
strFolderpath = strFolderpath &
"OLAttachments"
MsgBox strFolderpath
For
Each
objMsg
In
objSelection
Set
objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
MsgBox objAttachments.Count
If
lngCount > 0
Then
For
i = lngCount
To
1
Step
-1
strFile = objAttachments.Item(i).FileName
strFile = strFolderpath & strFile
objAttachments.Item(i).SaveAsFile strFile
objAttachments.Item(i).Delete
If
objMsg.BodyFormat <> olFormatHTML
Then
strDeletedFiles = strDeletedFiles & vbCrLf &
"<file://"
& strFile &
">"
Else
strDeletedFiles = strDeletedFiles &
"<br>"
&
"<a href='file://"
& _
strFile &
"'>"
& strFile &
"</a>"
End
If
MsgBox strDeletedFiles
Next
i
If
objMsg.BodyFormat <> olFormatHTML
Then
objMsg.Body = objMsg.Body & vbCrLf & _
"The file(s) were saved to "
& strDeletedFiles
Else
objMsg.HTMLBody = objMsg.HTMLBody &
"<p>"
& _
"The file(s) were saved to "
& strDeletedFiles &
"</p>"
End
If
objMsg.Save
strDeletedFiles =
""
End
If
Next
ExitSub:
Set
objAttachments =
Nothing
Set
objMsg =
Nothing
Set
objSelection =
Nothing
Set
objOL =
Nothing
End
Sub