Macro Move E-mail attachments to a PC Folder

Status
Not open for further replies.

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
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
S Outlook Macro to move reply mail based on the key word in the subjectline Outlook VBA and Custom Forms 0
Douglas Littlefield Macro to move E-mail Outlook VBA and Custom Forms 10
Douglas Littlefield Macro to move mail from inbox to Managed Folder Exchange Server Administration 1
J Macro to move mail from Inbox if older than 4 days Using Outlook 4
T Macro to move reply and original message to folder Outlook VBA and Custom Forms 6
witzker Macro to move @domain.xx of a Spammail to Blacklist in Outlook 2019 Outlook VBA and Custom Forms 7
S Macro to move “Re:” & “FWD:” email recieved the shared inbox to a subfolder in outlook Outlook VBA and Custom Forms 0
Eike Move mails via macro triggered by the click of a button? Outlook VBA and Custom Forms 0
N Macro to move all recipients to CC while replying Outlook VBA and Custom Forms 0
B Macro to manually move selected emails to network folder Outlook VBA and Custom Forms 1
G email returns after running macro to move emails Outlook VBA and Custom Forms 1
S Macro to print & move selected emails? Using Outlook 3
G Macro to move sent items from local folder to IMAP folder Using Outlook 4
S Outlook macro to move replied / forwarded emails to a seperate folder Using Outlook 1
C Help with a Macro to move emails to a different PST data file Using Outlook 4
A Outlook 2016 Macro to Reply, ReplyAll, or Forward(but with composing new email) Outlook VBA and Custom Forms 0
J Macro to Insert a Calendar Outlook VBA and Custom Forms 8
W Macro to Filter Based on Latest Email Outlook VBA and Custom Forms 6
D Autosort macro for items in a view Outlook VBA and Custom Forms 2
S HTML to Plain Text Macro - Help Outlook VBA and Custom Forms 1
A Macro to file emails into subfolder based on subject line Outlook VBA and Custom Forms 1
N Help creating a VBA macro with conditional formatting to change the font color of all external emails to red Outlook VBA and Custom Forms 5
S Visual indicator of a certain property or to show a macro toggle Outlook VBA and Custom Forms 2
L Modifying VBA script to delay running macro Outlook VBA and Custom Forms 3
S Macro to extract and modify links from emails Outlook VBA and Custom Forms 3
M Replyall macro with template and auto insert receptens Outlook VBA and Custom Forms 1
L Macro to add Date & Time etc to "drag to save" e-mails Outlook VBA and Custom Forms 8
S Macro for Loop through outlook unread emails Outlook VBA and Custom Forms 2
Globalforester ItemAdd Macro - multiple emails Outlook VBA and Custom Forms 3
S Macro to extract email addresses of recipients in current drafted email and put into clipboard Outlook VBA and Custom Forms 2
witzker HowTo start a macro with an Button in OL contact form Outlook VBA and Custom Forms 12
S Macro for other actions - Outlook 2007 Outlook VBA and Custom Forms 23
C Macro to extract sender name & subject line of incoming emails to single txt file Outlook VBA and Custom Forms 3
L Macro/VBA to Reply All, with the original attachments Outlook VBA and Custom Forms 3
S Outlook Macro to send auto acknowledge mail only to new mails received to a specific shared inbox Outlook VBA and Custom Forms 0
S Macro or plug-in to see if specific person was included in this email Outlook VBA and Custom Forms 4
U Macro for reminders,tasks,calendar Outlook VBA and Custom Forms 4
V macro runs slower on startup than after Outlook VBA and Custom Forms 3
A VBA macro for 15 second loop in send and received just for 1 specific mailbox Outlook VBA and Custom Forms 1
G VBA Macro Using Outlook 4
R Help Revising VBA macro to delete email over different time span Outlook VBA and Custom Forms 0
M Outlook macro to automate search and forward process Outlook VBA and Custom Forms 6
R Macro Schedule every day in Outlook Using Outlook 0
L Moving emails with similar subject and find the timings between the emails using outlook VBA macro Outlook VBA and Custom Forms 1
Healy Consultants Macro to remove inside organization distribution list email address when reply to all recepients Outlook VBA and Custom Forms 0
geofferyh Cannot get Macro to SAVE more than one message attachment??? Outlook VBA and Custom Forms 5
N How can I increase/faster outlook VBA Macro Speed ? Using Outlook 2
4 Macro to set the category of Deleted Item? Outlook VBA and Custom Forms 2
D.Moore Folder view settings by VBA macro Outlook VBA and Custom Forms 57
A Outlook macro to create search folder with mail categories as criteria Outlook VBA and Custom Forms 3

Similar threads

Top