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.
Thread starter Similar threads Forum Replies Date
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 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
C Outlook VBA - Create Macro Move Contact field Item to Another Using Outlook 1
S Macro or plug-in to see if specific person was included in this email Outlook VBA and Custom Forms 3
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 Calendar Printing Assistant 3
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
Dave A Run macro on existing appointment when it changes Outlook VBA and Custom Forms 1
V Outlook Macro to show Flagged messages Outlook VBA and Custom Forms 2
O Run macro automatically at sending an email Using Outlook 11
R Retain Original Message When Forwarding With Macro Outlook VBA and Custom Forms 3
C Macro to add multiple recipients to message Outlook VBA and Custom Forms 3
B Reply and replyall macro is not working Outlook VBA and Custom Forms 1
O Macro - paste as plain text Outlook VBA and Custom Forms 2
J Help Please!!! Outlook 2016 - VBA Macro for replying with attachment in meeting invite Outlook VBA and Custom Forms 9
witzker Macro to set contact reminder to next day 9:00 Outlook VBA and Custom Forms 45
M Adding Macro to populate "to" "subject" "body" not deleting email string below. Outlook VBA and Custom Forms 5
E Copying data from e-mail attachement to EXCEL file via macro Outlook VBA and Custom Forms 38
M Macro to add date/time stamp to subject Outlook VBA and Custom Forms 4
R VBA macro - new message Outlook VBA and Custom Forms 3
S Example VBA Macro - To Conditionally Change the From Account and Add a BCC Address on Emails Outlook VBA and Custom Forms 11
S Macro using .SendUsingAccount only works the first time, after starting Outlook Outlook VBA and Custom Forms 4
S VBA Macro - Run-time error '424': object required - Help Please Outlook VBA and Custom Forms 3
B VBA Macro for assigning multiple Categories to an email in my Inbox Outlook VBA and Custom Forms 1
N Macro for attachment saved and combine Outlook VBA and Custom Forms 1
Sabastian Samuel HOW DO I FORWARD AN EMAIL WITH MACRO using an email that in the body of another email Outlook VBA and Custom Forms 3
C Search with Google Macro? Outlook VBA and Custom Forms 4
J Outlook 2013 Extract Flag Completed dates to Excel Macro Outlook VBA and Custom Forms 16
M Slow VBA macro in Outlook Outlook VBA and Custom Forms 5
D Print Attachments only in selected emails using a macro Outlook VBA and Custom Forms 3
M Macro for attachments download adjustment Outlook VBA and Custom Forms 3
Similar threads


















































Top