Macro Move E-mail attachments to a PC Folder

LMS

Senior Member
Outlook version
Outlook 2007
Email Account
Exchange Server
Since I receive emails with attachments, I read a code from you that I don't understand that copies all attachments of an email to a folder. The folder I want to use is C:users.username.documents.clients.clientname. Can you please give me the full code?
 

LMS

Senior Member
Outlook version
Outlook 2007
Email Account
Exchange Server
Update in this area please?
 

LMS

Senior Member
Outlook version
Outlook 2007
Email Account
Exchange Server
Update please? And here is the code you told all...and i changed the location of the folder...so please look at this and tell me what to change please specifically.

Public Sub SaveAttachments()
Dim objOL As outlook.Application
Dim objMsg As outlook.mailItem 'Object
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

' Get the path to your My Documents folder
strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)

On Error Resume Next

' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")

' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection

' The attachment folder needs to exist
' You can change this to another folder name of your choice

' Set the Attachment folder.
strFolderpath = strFolderpath & "C:\Users\Louis\Documents\OLAttachments"

' Check each selected item for attachments.
For Each objMsg In objSelection

Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count

If lngCount > 0 Then

' Use a count down loop for removing items
' from a collection. Otherwise, the loop counter gets
' confused and only every other item is removed.

For i = lngCount To 1 Step -1

' Get the file name.
strFile = objAttachments.Item(i).FileName

' Save the attachment as a file.
objAttachments.Item(i).SaveAsFile strFile

Next i
End If

Next

ExitSub:

Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing

End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
So you want to create a folder using the client's name under OLAttachments folder?

You'll need to get the objMsg.SenderName and add it to strFolderPath. Adding it to the filename *should* create the folder if it doesn't exist (since it's last in the path). I'll double check that.

' Check each selected item for attachments.
For Each objMsg In objSelection
strSender = objMsg.SenderName
' Set the Attachment folder.
strFolderpath = strFolderpath & "C:\Users\Louis\Documents\OLAttachments\" & strSender & "\"

Also - isn't this redundant?

strFolderpath = strFolderpath & "C:\Users\Louis\Documents\OLAttachments"
strFolderPath should be "C:\Users\Louis\Documents"
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Ok... you do need to use filescripting object ot create the folder

Code:
' Set the Attachment folder. 
strFolderpath = strFolderpath & "\OLAttachments\" 
 
' Check each selected item for attachments. 
For Each objMsg In objSelection 
strSender = objMsg.SenderName 
strFolderpath = strFolderpath & "\" & strSender 
 
Set fso = CreateObject("Scripting.FileSystemObject") 
If Not fso.FolderExists(strFolderpath) Then 
            fso.CreateFolder (strFolderpath) 
End If
and when you put it together at the end, make sure you use a slash before the filename -

objAttachments.Item(i).SaveAsFile strFolderpath & "\" & strFile
 

LMS

Senior Member
Outlook version
Outlook 2007
Email Account
Exchange Server
Ok... you do need to use filescripting object ot create the folder

Code:
' Set the Attachment folder. 
strFolderpath = strFolderpath & "\OLAttachments\" 
 
' Check each selected item for attachments. 
For Each objMsg In objSelection 
strSender = objMsg.SenderName 
strFolderpath = strFolderpath & "\" & strSender 
 
Set fso = CreateObject("Scripting.FileSystemObject") 
If Not fso.FolderExists(strFolderpath) Then 
            fso.CreateFolder (strFolderpath) 
End If
and when you put it together at the end, make sure you use a slash before the filename -

objAttachments.Item(i).SaveAsFile strFolderpath & "\" & strFile
 

LMS

Senior Member
Outlook version
Outlook 2007
Email Account
Exchange Server
Thanks so much. I don't understand the concepts. Can you please simply post the fulk code for me to copy and test?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Code:
Public Sub SaveAttachments() 
Dim objOL As outlook.Application 
Dim objMsg As outlook.mailItem 'Object 
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 
 
' Get the path to your My Documents folder 
strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16) 
 
On Error Resume Next 
 
' Instantiate an Outlook Application object. 
Set objOL = CreateObject("Outlook.Application") 
 
' Get the collection of selected objects. 
Set objSelection = objOL.ActiveExplorer.Selection 
 
' The attachment folder needs to exist 
' You can change this to another folder name of your choice 
 
' Set the Attachment folder. 
strFolderpath = strFolderpath & "\OLAttachments\" 
 
' Check each selected item for attachments. 
For Each objMsg In objSelection 
strSender = objMsg.SenderName 
strFolderpath = strFolderpath & "\" & strSender 
 
Set fso = CreateObject("Scripting.FileSystemObject") 
If Not fso.FolderExists(strFolderpath) Then 
fso.CreateFolder (strFolderpath) 
End If 
 
Set objAttachments = objMsg.Attachments 
lngCount = objAttachments.Count 
 
If lngCount > 0 Then 
 
' Use a count down loop for removing items 
' from a collection. Otherwise, the loop counter gets 
' confused and only every other item is removed. 
 
For i = lngCount To 1 Step -1 
 
' Get the file name. 
strFile = objAttachments.Item(i).FileName 
 
' Save the attachment as a file. 
objAttachments.Item(i).SaveAsFile strFolderpath & "\" & strFile 
Next i 
End If 
 
Next 
 
ExitSub: 
 
Set objAttachments = Nothing 
Set objMsg = Nothing 
Set objSelection = Nothing 
Set objOL = Nothing 
 
End Sub
 

LMS

Senior Member
Outlook version
Outlook 2007
Email Account
Exchange Server
Wonderful....I added a folder to my Documents named: OLAttachments.....then select the email that has the attachment...and it creates a folder inside the folder OLAttachments with the name of the sender....and inside that subfolder, there is the attachment....

so if all I want to do is to put the attachment in a folder which is a folder in the folder Clients. C:\Users\Louis\Documents\Clients\ClientName
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
I'm assuming ClientName is the client's actual name? Where does it come from?

this line sets the path:
strFolderpath = strFolderpath & "\OLAttachments\"
strFolderpath is C:\Users\Louis\Documents, so
strFolderpath = strFolderpath & "\Clients\" = C:\Users\Louis\Documents\Clients\
 

LMS

Senior Member
Outlook version
Outlook 2007
Email Account
Exchange Server
I'm assuming ClientName is the client's actual name? Where does it come from?

this line sets the path:
strFolderpath = strFolderpath & "\OLAttachments\"
strFolderpath is C:\Users\Louis\Documents, so
strFolderpath = strFolderpath & "\Clients\" = C:\Users\Louis\Documents\Clients\
Once again, not my expertise. Can you change the code you just posted that i tested and i will replace the word ClientName with a folder name of a client?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
If you aren't creating the name dynamically (like from the From field of the message), you just need to enter the name in the path, like this.

strFolderpath = strFolderpath & "\Clients\ClientName\"
 

LMS

Senior Member
Outlook version
Outlook 2007
Email Account
Exchange Server
Thanks very much. Will test it at the office tomorrow and let you know.
 

LMS

Senior Member
Outlook version
Outlook 2007
Email Account
Exchange Server
I just tried it and it works perfectly.....and can even add more subfolders to where it goes...

So the only other question is what to delete from the code so it just puts the attachments in the folder but not create a new folder of the name of the sender? Thank would be great to see what to delete please right away.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Use the original code and change the strfolderpath line:
strFolderpath = strFolderpath & "\Clients\ClientName\"
 

LMS

Senior Member
Outlook version
Outlook 2007
Email Account
Exchange Server
I just tried it and it works perfectly.....and can even add more subfolders to where it goes...

So the only other question is what to delete from the code so it just puts the attachments in the folder but not create a new folder of the name of the sender? Thank would be great to see what to delete please right away.
 

LMS

Senior Member
Outlook version
Outlook 2007
Email Account
Exchange Server
I just tried it and it works perfectly.....and can even add more subfolders to where it goes...

So the only other question is what to delete from the code so it just puts the attachments in the folder but not create a new folder of the name of the sender? Thank would be great to see what to delete please right away.

I just deleted the following and not a new folder....just the attachements

strSender = objMsg.SenderName
strFolderpath = strFolderpath & "\" & strSender
 
Top