Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #8  
Old 07-03-2014, 04:59 PM
niton niton is offline Save attachments to defined path via VBA Windows 7 64bit Save attachments to defined path via VBA Office 2010 64bit
Competent Performer
 
Join Date: Jul 2012
Posts: 102
niton is on a distinguished road
Default

The original code works on a selection of one or more mailitems in any folder.

To run as a script on mail being received try this:

Code:
 
Sub SaveAttachments_ReceivedMail(item As mailItem)
Dim ItemAttachment As Object
Dim StrFolderPath As String
Dim strFileName As String
Dim ItemsAttachmentsCount As Long
Dim iSave As Long
Dim msg As String
StrFolderPath = "C:\test\"
If (Dir$(StrFolderPath, vbDirectory) = "") Then
    Debug.Print "'" & StrFolderPath & "' not exist"
    MkDir StrFolderPath
    Debug.Print "'" & StrFolderPath & "' we create it"
Else
    Debug.Print "'" & StrFolderPath & "' exist"
End If
If Right(StrFolderPath, 1) <> "\" Then
   StrFolderPath = StrFolderPath & "\"
End If
ItemsAttachmentsCount = 0
 
    If TypeOf item Is mailItem Then
 
        For Each ItemAttachment In item.Attachments
            ItemsAttachmentsCount = ItemsAttachmentsCount + 1
            ' Get the file name.
            strFileName = ItemAttachment.FileName
            ' Combine with the path to the Attachments folder.
            strFileName = StrFolderPath & ItemsAttachmentsCount & "_" & strFileName
            ' Save the attachment as a file.
            ItemAttachment.SaveAsFile strFileName
        Next ItemAttachment
 
    End If
 
ExitSub:
Set item = Nothing
msg = "All Selected Folder Attachments Have Been Saved to " & StrFolderPath & vbCr & vbCr
msg = msg & "ItemsAttachmentsCount : " & ItemsAttachmentsCount
Debug.Print msg
End Sub
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Save attachments to defined path via VBA FileSystemObject Compile Error: User-Defined Type Not Defined gsrikanth Excel Programming 2 03-28-2022 06:32 AM
Save attachments to defined path via VBA Range.Information(wdStartOfRangeRowNumber): Application-defined or Object-defined err tinfanide Excel Programming 2 06-09-2012 10:19 AM
Save attachments to defined path via VBA Application-defined or Object-defined error Manit Excel Programming 4 12-08-2011 07:35 PM
Outlook 2007 prompts to save opened attachments OfficeAppentice Outlook 0 11-03-2011 09:24 AM
Save attachments to defined path via VBA code to save / rename / send attachments unit213 Outlook 1 09-26-2007 08:15 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:07 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft