Modification Needed on this Macro

krishhi

Active Member
Joined
Sep 8, 2008
Messages
328
Hi Guys,

I have this macro, it will download the attachemnts to a particular folder from the selected mail. But I want to edit the macro like below specifications.

1. Macro should ask the ask user for the parth.
2. When it is saving attachemnts, it should save the attachemnts In a New Folder and Name the Folder with "Mail Subject"

Here is the Code:

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
    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
    
    Next
    
ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub


Waiting for the Kind Reply,

Krrish
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Give this a try:
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

'Folder selection
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .InitialFileName = "C:\"
        .Show
        If .SelectedItems.Count > 0 Then strFolderpath = .SelectedItems(1) & "\"
    End With
    
'create new target folder in chosen path
    On Error Resume Next
' Set the Attachment folder.
    strFolderpath = strFolderpath & "\Mail Subject\"
    MkDir strFolderpath
    
' Instantiate an Outlook Application object.
    Set objOL = CreateObject("Outlook.Application")
' Get the collection of selected objects.
    Set objSelection = objOL.ActiveExplorer.Selection
' 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
        
        ' 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 objMsg
    
ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
 
Upvote 0
<font face=Courier New><SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Sub</SPAN> SaveAttachments()<br><SPAN style="color:#00007F">Dim</SPAN> objOL <SPAN style="color:#00007F">As</SPAN> Outlook.Application<br><SPAN style="color:#00007F">Dim</SPAN> objMsg <SPAN style="color:#00007F">As</SPAN> Outlook.MailItem <SPAN style="color:#007F00">'Object</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> objAttachments <SPAN style="color:#00007F">As</SPAN> Outlook.Attachments<br><SPAN style="color:#00007F">Dim</SPAN> objSelection <SPAN style="color:#00007F">As</SPAN> Outlook.Selection<br><SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> lngCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> strFile <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> strFolderpath <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> strSubfolder <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> strDeletedFiles <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> InvalidPathChars <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br><br>InvalidPathChars = Array("*", "|", "/", "\", ":", """", "<", ">", ".", "?")<br><br>    <SPAN style="color:#007F00">' Get the path to your My Documents folder</SPAN><br><SPAN style="color:#007F00">'    strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)</SPAN><br>    <br>    <SPAN style="color:#007F00">' Prompt user to select a folder</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> Application.FileDialog(msoFileDialogFolderPicker)<br>        .InitialFileName = CreateObject("WScript.Shell").SpecialFolders(16) <SPAN style="color:#007F00">' Default path</SPAN><br>        .Title = "Please Select a Destination Folder"<br>        .ButtonName = "Select Folder"<br>        .AllowMultiSelect = <SPAN style="color:#00007F">False</SPAN><br>        .Show<br>        <SPAN style="color:#00007F">If</SPAN> .SelectedItems.Count = 0 <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>   <SPAN style="color:#007F00">' User clicked cancel</SPAN><br>        strFolderpath = .SelectedItems.Item(1) & Application.PathSeparator<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <br>    <SPAN style="color:#007F00">'On Error Resume Next</SPAN><br>    <br>    <SPAN style="color:#007F00">' Instantiate an Outlook Application object.</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> objOL = CreateObject("Outlook.Application")<br>    <SPAN style="color:#007F00">' Get the collection of selected objects.</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> objSelection = objOL.ActiveExplorer.Selection<br><SPAN style="color:#007F00">' The attachment folder needs to exist</SPAN><br><SPAN style="color:#007F00">' You can change this to another folder name of your choice</SPAN><br>    <SPAN style="color:#007F00">' Set the Attachment folder.</SPAN><br><SPAN style="color:#007F00">'    strFolderpath = strFolderpath & "\OLAttachments\"</SPAN><br>    <SPAN style="color:#007F00">' Check each selected item for attachments.</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> objMsg <SPAN style="color:#00007F">In</SPAN> objSelection<br>        <SPAN style="color:#00007F">Set</SPAN> objAttachments = objMsg.Attachments<br>        lngCount = objAttachments.Count<br>        <SPAN style="color:#00007F">If</SPAN> lngCount > 0 <SPAN style="color:#00007F">Then</SPAN><br>            <SPAN style="color:#007F00">' Use a count down loop for removing items</SPAN><br>            <SPAN style="color:#007F00">' from a collection. Otherwise, the loop counter gets</SPAN><br>            <SPAN style="color:#007F00">' confused and only every other item is removed.</SPAN><br>            <br>            <SPAN style="color:#007F00">'create sub folder</SPAN><br>            strSubfolder = Left(objMsg.Subject, 20) <SPAN style="color:#007F00">'Limit folder name to 20 characters</SPAN><br>            <SPAN style="color:#00007F">For</SPAN> i = 0 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(InvalidPathChars)<br>                strSubfolder = Replace(strSubfolder, InvalidPathChars(i), "_")<br>            <SPAN style="color:#00007F">Next</SPAN> i<br>            strSubfolder = strSubfolder & Application.PathSeparator<br>            <SPAN style="color:#00007F">If</SPAN> Dir(strFolderpath & strSubfolder) = "" <SPAN style="color:#00007F">Then</SPAN> MkDir strFolderpath & str<SPAN style="color:#00007F">Sub</SPAN>folder<br>            <br>            <SPAN style="color:#00007F">For</SPAN> i = lngCount <SPAN style="color:#00007F">To</SPAN> 1 <SPAN style="color:#00007F">Step</SPAN> -1<br>                <SPAN style="color:#007F00">' Get the file name.</SPAN><br>                strFile = objAttachments.Item(i).Filename<br>                <SPAN style="color:#007F00">' Save the attachment as a file.</SPAN><br>                objAttachments.Item(i).SaveAsFile strFolderpath & strSubfolder & strFile<br>            <SPAN style="color:#00007F">Next</SPAN> i<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN><br>    <br>ExitSub:<br><SPAN style="color:#00007F">Set</SPAN> objAttachments = <SPAN style="color:#00007F">Nothing</SPAN><br><SPAN style="color:#00007F">Set</SPAN> objMsg = <SPAN style="color:#00007F">Nothing</SPAN><br><SPAN style="color:#00007F">Set</SPAN> objSelection = <SPAN style="color:#00007F">Nothing</SPAN><br><SPAN style="color:#00007F">Set</SPAN> objOL = <SPAN style="color:#00007F">Nothing</SPAN><br><SPAN style="color:#00007F">End</SPAN> Sub</FONT>
 
Upvote 0
jbeaucaire AND AlphaFrog


Thank you very much guys for your reply, But When i am trying to run these code, i am getting error, at this point.

Code:
With Application.FileDialog(msoFileDialogFolderPicker)



Error Message:

Run-time error '438':
Object doesn't support this property or method

By the way i am using Microsoft Office 2007.

Waiting for your kind reply
 
Upvote 0
That portion of the code works for me (Excel 2010 and it should work at least back to Excel 2003). Do you still get that error with

Code:
Sub atest()
Dim strFolderpath As String
 With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .InitialFileName = "C:\"
        .Show
        If .SelectedItems.Count > 0 Then strFolderpath = .SelectedItems(1) & "\"
    End With
MsgBox strFolderpath
End Sub
 
Upvote 0
That portion of the code works for me (Excel 2010 and it should work at least back to Excel 2003). Do you still get that error with

Code:
Sub atest()
Dim strFolderpath As String
 With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .InitialFileName = "C:\"
        .Show
        If .SelectedItems.Count > 0 Then strFolderpath = .SelectedItems(1) & "\"
    End With
MsgBox strFolderpath
End Sub

VOG,

Thank you very much for the reply, I am using Office 2007, the code won't work in that. It gives the error as i said above? I have outlook express, but there is no option for macro [vba editor] to paste that code
 
Upvote 0
Here is a screenshot of the error i am getting
error.jpg
 
Upvote 0
I don't know why you get that error.

As a quick test, try this...

<font face=Courier New>    <SPAN style="color:#007F00">' Prompt user to select a folder</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> Application.FileDialog(4)<br>    <SPAN style="color:#007F00">'With Application.FileDialog(msoFileDialogFolderPicker)</SPAN><br>        <SPAN style="color:#007F00">'.InitialFileName = CreateObject("WScript.Shell").SpecialFolders(16) ' Default path</SPAN><br>        .Title = "Please Select a Destination Folder"<br>        .ButtonName = "Select Folder"<br>        .AllowMultiSelect = <SPAN style="color:#00007F">False</SPAN><br>        .Show<br>        <SPAN style="color:#00007F">If</SPAN> .SelectedItems.Count = 0 <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>   <SPAN style="color:#007F00">' User clicked cancel</SPAN><br>        strFolderpath = .SelectedItems.Item(1) & Application.PathSeparator<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN></FONT>

It's a guess. If it doesn't work, go back to the original.

Under the VBA menu Tools\ References, what references do you have checked? Please list all.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,758
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top