• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Code for outlook attachment

Veeru106

Member
Hi,


I was using a code to automatically save my reports in my documents folder in their respective folder.


But it is not executing correctly for some reason. I guess it is because , it is not able read my document path.


I am using below line code in my code



' Get the path to your My Documents folder

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

Debug.Print strFolderpath

On Error Resume Next



Can any one suggest…is this number 16 is correct or how do I find out my document path.


Thanks
 
Hi, Veeru106!

Please don't post code as text, use the code tags as indicated on the very top of the comment text box, and available at the ribbon just below the tip.

And this is not the first time you get advised about it:
http://chandoo.org/forum/threads/copy-and-paste.34712/#post-207160

You can get the path to MyDocuments with this:
Code:
Environ("HOMEDRIVE") & Environ("HOMEPATH") & "\Documents"

Regards!
 
Last edited:
Code:
Dim strFolder As String

Public Sub SaveToFolderBob()
    strFolder = "For T&E"
    SaveAttachments
End Sub

Public Sub SaveToFolderJim()
    strFolder = "For President"
    SaveAttachments
End Sub

Private 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)
    Debug.Print strFolderpath
    On Error Resume Next

' The attachment folder needs to exist
' You can change this to another folder name of your choice

    ' Set the Attachment folder.
    strFolderpath = strFolderpath & "\" & strFolder & "\"
    Debug.Print strFolderpath

    Set objOL = Outlook.Application
    Set objMsg = objOL.ActiveExplorer.Selection.Item(1)

    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
   
    ' 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
       
ExitSub:

Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
 
Sorry for the confusion...I will take it in mind going forward....I am using above code to save attachments from outlook to my documents directly...I have created to folders with name of "For T&E" and "For President". Same folders has been saved in my documents...it should suppose to save any file when I click on mail , which contain that file and then click on command button created in outlook..but it not saving anyting nor it showing any error message..Please suggest what is missing here..Thanks
 
Another thing..when I try to run a macro...this time it gives me error..Macro in this project are disabled.Please refer to online help to determine how to enable macros.
 
Hi...I have tried to use code given by you above but it is not working as well.

May be I am not applying it at right place..
Code:
Environ("HOMEDRIVE") & Environ("HOMEPATH") & "\Documents"
.

Can anyone take a look please...
Thanks
 
Hi, Veeru106!
Try replacing this:
Code:
 strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
by:
Code:
 strFolderpath = Environ("HOMEDRIVE") & Environ("HOMEPATH") & "\Documents"
Regards!
 
I have tried to replace it but still attachment is not going & saving in my documents folder...I have also tried to change my Macro settings in trust settings but still no results...Currently it in enable all Marcos (no Recommended )..thanks
 
Hi, Veeru106!
Consider uploading a fully working sample file, with the indications of how to test it. It'd be easier to understand for people who might be able to help you.
Regards!
 
Greetings for the day!! and sorry for coming back on this but can we have further changes to make it more dynamic...Right now it is saving files in my documents in that respective folder,.....but can we also create something where before saving file, it also create a next month folder and save that file in that folder only.......Please suggest...thanks
 
Hi, Veeru106!
I'm afraid I won't be of much help in this case. I can assist you with VBA, folder names and so, but I don't use Outlook so I can't even test your posted code.
As a last try, describe with examples the folder structure you're dealing with, e.g., last month folder, this month folder, next month folder, parent folder, and any other you think that are relevant.
Regards!
 
Ok I want folder to be created according to the date of email. lets say if I am saving a attachment and email date is 5th june 2017...it should create a June month folder first and then save this attachment in that folder...Please suggest if you can...or guide me where I can find this solution....thanks
 
Hi, Veeru106!
And where should created those monthly folders, under which parent folder?
Regards!
 
I am creating these under my documents in different folders like if you go back to my code provided above I have created 2 folders in my documents "For T&E" and "For President"....I want file to be saved under these with monthly folders
 
And where should created those monthly folders, under which parent folder?
Hi, Veeru106!
You haven't answered this, please post the whole path starting from drive letter and ending with file extension.
Regards!
 
Back
Top