outlook VBA save attachment on hard drive to a specific folder different of 'my documents folder'

0

I've found a macro which save an attachment from an email on outlook, and in this aspect it works pretty fine, but it saves it in "my document" folder

I would like that it saves it in another folder which have a completely different path, however I have trouble to see how the code which do that works and thus i don't suceed in modifying it

The current path for my documents folder is C:\Users\me\Documents\OLAttachments but it should be Y:\work_network\me\outlook-file

The current code i have is

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

' Set the Attachment folder.
strFolderpath = strFolderpath & "\OLAttachments\"

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

Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count

If lngCount > 0 Then
For i = lngCount To 1 Step -1

' Get the file name.
strFile = Left(objAttachments.Item(i).FileName, Len(stry) - 4) & Format(Date, "DDMMYY") & ".xls"

' Combine with the path to the Temp folder.
strFile = strFolderpath & strFile

' 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

I don't really understand how this line works

strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)

And i have troubles to adapt it to what i need.

Can someone provide some help on that? Thanks a lot

ploom

Posted 2016-08-25T13:45:12.390

Reputation: 1

Well, you found a macro. What did you try changing? How didn't it work? What did it do? What did you want it to do? – Raystafarian – 2016-08-25T14:07:44.037

change is ' Set the Attachment folder. strFolderpath = strFolderpath & "\OLAttachments" change to strFolderpath="Y:\work_network\me\outlook-file" – bvaughn – 2016-08-25T14:22:47.047

Well i tried smtg like strFolderpath = CreateObject("WScript.Shell").GetFolderPath("Y:\work_network\me\outlook-file") and directly strFolderpath = "Y:\work_network\me\outlook-file") but both didn't work. I'm not very experienced with VBA that's why i went looking for a pre written code haha Well I only wanted to change the final folder where my attachment is saved but i don't know how to manipulate the CreateObject("WScript.Shell") structure Maybe the path should have a totally different struture but i don't know how to do it – ploom – 2016-08-25T14:26:01.127

1 - Put Option Explicit at the top of your code. There is a setting in Tools|Options|Editor tab "Require Variable Declaration". 2 - You cannot debug when On Error Resume Next is used indiscriminately. Remove it. Either of these two actions would have revealed the variable stry is unknown. The answer provided by thims should be the correct answer to your question. – niton – 2016-08-29T19:32:26.260

Answers

1

The mentioned line gets the path for "My Documents" special folder. You just need to replace this line with:

strFolderpath = "Y:\work_network\me\outlook-file\"

and completely remove this line:

strFolderpath = strFolderpath & "\OLAttachments\"

thims

Posted 2016-08-25T13:45:12.390

Reputation: 8 081

I tried it but it doesn't work I don't see anything in my folder and have no error msg either :/ – ploom – 2016-08-25T14:41:15.020

If it works with "My Documents" it has to work with this path. Try to debug your script. – thims – 2016-08-25T17:00:39.340