Macro Move E-mail attachments to a PC Folder

Status
Not open for further replies.

LMS

Senior Member
Outlook version
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?
 
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
 
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"
 
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
 
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
 
Thanks so much. I don't understand the concepts. Can you please simply post the fulk code for me to copy and test?
 
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
 
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
 
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\
 
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?
 
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\"
 
Thanks very much. Will test it at the office tomorrow and let you know.
 
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.
 
Use the original code and change the strfolderpath line:
strFolderpath = strFolderpath & "\Clients\ClientName\"
 
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 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
N Line to move origEmail to subfolder within a reply macro Outlook VBA and Custom Forms 0
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
X Run macro automatically when a mail appears in the sent folder Using Outlook 0
mrrobski68 Issue with Find messages in a conversation macro Outlook VBA and Custom Forms 1
G Creating Macro to scrape emails from calendar invite body Outlook VBA and Custom Forms 6
M Use Macro to change account settings Outlook VBA and Custom Forms 0
J Macro to Reply to Emails w/ Template Outlook VBA and Custom Forms 3
C Outlook - Macro to block senders domain - Macro Fix Outlook VBA and Custom Forms 2
Witzker Outlook 2019 Macro to seach in all contact Folders for marked Email Adress Outlook VBA and Custom Forms 1
S macro error 4605 Outlook VBA and Custom Forms 0
A Macro Mail Alert Using Outlook 4
J Outlook 365 Outlook Macro to Sort emails by column "Received" to view the latest email received Outlook VBA and Custom Forms 0
J Macro to send email as alias Outlook VBA and Custom Forms 0
M Outlook Macro to save as Email with a file name format : Date_Timestamp_Sender initial_Email subject Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro GoTo user defined search folder Outlook VBA and Custom Forms 6
D Outlook 2016 Creating an outlook Macro to select and approve Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro to send an Email Template from User Defined Contact Form Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro to check Cursor & Focus position Outlook VBA and Custom Forms 8
V Macro to mark email with a Category Outlook VBA and Custom Forms 4
M Outlook 2019 Macro not working Outlook VBA and Custom Forms 0
S Outlook 365 Help me create a Macro to make some received emails into tasks? Outlook VBA and Custom Forms 1
Geldner Send / Receive a particular group via macro or single keypress Using Outlook 1
D Auto Remove [EXTERNAL] from subject - Issue with Macro Using Outlook 21
V Macro to count flagged messages? Using Outlook 2
sophievldn Looking for a macro that moves completed items from subfolders to other subfolder Outlook VBA and Custom Forms 7
S Outlook Macro for [Date][Subject] Using Outlook 1
E Outlook - Macro - send list of Tasks which are not finished Outlook VBA and Custom Forms 3
E Macro to block senders domain Outlook VBA and Custom Forms 1
D VBA Macro to Print and Save email to network location Outlook VBA and Custom Forms 1
N VBA Macro To Save Emails Outlook VBA and Custom Forms 1
Witzker Outlook 2019 Macro to answer a mail with attachments Outlook VBA and Custom Forms 2
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

Similar threads

Back
Top