PDA

View Full Version : Need Modification On this Macro



krishhi
08-23-2011, 10:25 PM
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 path.

2. When it is saving attachemnets, it should save the attachemnts In a New Folder and Name the Folder with "Mail Subject"

Here is the Code:

Here is the code currently i have,

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

Thank in advance,
Waiting for your kind reply

Charlize
08-26-2011, 07:06 AM
Take a look at this kb article : http://www.vbaexpress.com/kb/getarticle.php?kb_id=559

Charlize

JP2112
08-29-2011, 12:26 PM
1. Macro should ask the ask user for the path.

Remove these lines:

strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
strFolderpath = strFolderpath & "\OLAttachments\"

See http://www.vbaexpress.com/kb/getarticle.php?kb_id=284 for VBA code to browse for folder. (Paste that code into your project)

Code should be:

strFolderpath = BrowseForFolder


2. When it is saving attachemnets, it should save the attachemnts In a New Folder and Name the Folder with "Mail Subject"

Right after "strFolderpath = BrowseForFolder" use this code:

MkDir strFolderpath & "\" & msg.Subject
strFolderpath = strFolderpath & "\" & msg.Subject