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

5,590

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\"
Share:
5,590

Related videos on Youtube

ploom
Author by

ploom

Updated on September 18, 2022

Comments

  • ploom
    ploom over 1 year

    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

    • Admin
      Admin over 7 years
      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?
    • Admin
      Admin over 7 years
      change is ' Set the Attachment folder. strFolderpath = strFolderpath & "\OLAttachments\" change to strFolderpath="Y:\work_network\me\outlook-file"
    • Admin
      Admin over 7 years
      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
    • Admin
      Admin over 7 years
      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.
  • ploom
    ploom over 7 years
    I tried it but it doesn't work I don't see anything in my folder and have no error msg either :/
  • thims
    thims over 7 years
    If it works with "My Documents" it has to work with this path. Try to debug your script.