A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Help to modify the code



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old December 19th 07, 06:52 PM posted to microsoft.public.outlook.program_vba
Boss
external usenet poster
 
Posts: 5
Default Help to modify the code

Hi,

Below is the code to download attachemnt from outlook.
I wish to name the saved file as the subject of the email.
Please help me.. i am poor in coding, i can just record macros.
This code i got from the community.

Thanks a lot. Its very imp for me.
Boss


Sub GetAttachments()
' This Outlook macro checks a the Outlook Inbox for messages
' with attached files (of any type) and saves them to disk.
' NOTE: make sure the specified save folder exists before
' running the macro.
On Error GoTo GetAttachments_err
' Declare variables
Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim i As Integer
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
i = 0
' Check Inbox for messages and exit of none found
If Inbox.Items.Count = 0 Then
MsgBox "There are no messages in the Inbox.", vbInformation, _
"Nothing Found"
Exit Sub
End If
' Check each message for attachments
For Each Item In Inbox.Items
' Save any attachments found
For Each Atmt In Item.Attachments
' This path must exist! Change folder name as necessary.
FileName = "C:\Email Attachments\" & Atmt.FileName
Atmt.SaveAsFile FileName
i = i + 1
Next Atmt
Next Item
' Show summary message
If i 0 Then
MsgBox "I found " & i & " attached files." _
& vbCrLf & "I have saved them into the C:\Email Attachments folder." _
& vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
Else
MsgBox "I didn't find any attached files in your mail.",
vbInformation, "Finished!"
End If
' Clear memory
GetAttachments_exit:
Set Atmt = Nothing
Set Item = Nothing
Set ns = Nothing
Exit Sub
' Handle errors
GetAttachments_err:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Macro Name: GetAttachments" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume GetAttachments_exit
End Sub

Ads
  #2  
Old December 19th 07, 07:36 PM posted to microsoft.public.outlook.program_vba
JP[_3_]
external usenet poster
 
Posts: 201
Default Help to modify the code

Change

FileName = "C:\Email Attachments\" & Atmt.FileName

to

FileName = "C:\Email Attachments\" & Item.Subject


Keep in mind this code may fail because you are declaring 'Item' as an
Object and AFAIK not all possible Inbox items have a 'Subject'
property.


HTH,
JP

On Dec 19, 1:52 pm, Boss wrote:
Hi,

Below is the code to download attachemnt from outlook.
I wish to name the saved file as the subject of the email.
Please help me.. i am poor in coding, i can just record macros.
This code i got from the community.

Thanks a lot. Its very imp for me.
Boss

Sub GetAttachments()
' This Outlook macro checks a the Outlook Inbox for messages
' with attached files (of any type) and saves them to disk.
' NOTE: make sure the specified save folder exists before
' running the macro.
On Error GoTo GetAttachments_err
' Declare variables
Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim i As Integer
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
i = 0
' Check Inbox for messages and exit of none found
If Inbox.Items.Count = 0 Then
MsgBox "There are no messages in the Inbox.", vbInformation, _
"Nothing Found"
Exit Sub
End If
' Check each message for attachments
For Each Item In Inbox.Items
' Save any attachments found
For Each Atmt In Item.Attachments
' This path must exist! Change folder name as necessary.
FileName = "C:\Email Attachments\" & Atmt.FileName
Atmt.SaveAsFile FileName
i = i + 1
Next Atmt
Next Item
' Show summary message
If i 0 Then
MsgBox "I found " & i & " attached files." _
& vbCrLf & "I have saved them into the C:\Email Attachments folder." _
& vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
Else
MsgBox "I didn't find any attached files in your mail.",
vbInformation, "Finished!"
End If
' Clear memory
GetAttachments_exit:
Set Atmt = Nothing
Set Item = Nothing
Set ns = Nothing
Exit Sub
' Handle errors
GetAttachments_err:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Macro Name: GetAttachments" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume GetAttachments_exit
End Sub


  #3  
Old December 19th 07, 08:35 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Help to modify the code

All Outlook items implement the Subject property.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"JP" wrote in message
...
Change

FileName = "C:\Email Attachments\" & Atmt.FileName

to

FileName = "C:\Email Attachments\" & Item.Subject


Keep in mind this code may fail because you are declaring 'Item' as an
Object and AFAIK not all possible Inbox items have a 'Subject'
property.


HTH,
JP


  #4  
Old December 20th 07, 10:31 AM posted to microsoft.public.outlook.program_vba
Boss
external usenet poster
 
Posts: 5
Default Help to modify the code

It worked.. many many many thanks.. It was really imp for me..

Gaurav

"Ken Slovak - [MVP - Outlook]" wrote:

All Outlook items implement the Subject property.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"JP" wrote in message
...
Change

FileName = "C:\Email Attachments\" & Atmt.FileName

to

FileName = "C:\Email Attachments\" & Item.Subject


Keep in mind this code may fail because you are declaring 'Item' as an
Object and AFAIK not all possible Inbox items have a 'Subject'
property.


HTH,
JP



  #5  
Old December 20th 07, 01:19 PM posted to microsoft.public.outlook.program_vba
JP[_3_]
external usenet poster
 
Posts: 201
Default Help to modify the code

Thanks Ken, couldn't remember.
--JP

On Dec 19, 3:35 pm, "Ken Slovak - [MVP - Outlook]"
wrote:
All Outlook items implement the Subject property.

--
Ken Slovak
[MVP - Outlook]http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Optionshttp://www.slovaktech.com/products.htm

"JP" wrote in message

...



Change


FileName = "C:\Email Attachments\" & Atmt.FileName


to


FileName = "C:\Email Attachments\" & Item.Subject


Keep in mind this code may fail because you are declaring 'Item' as an
Object and AFAIK not all possible Inbox items have a 'Subject'
property.


HTH,
JP

 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Modify a particular Appointment Atul Saxena Outlook - Calandaring 1 October 9th 07 01:17 PM
Modify Code Using Outlook Redemption to bypass security prompts mhgreene Outlook and VBA 5 October 3rd 07 10:50 PM
How to modify an AppointmentItem? OctopusThu Add-ins for Outlook 6 December 14th 06 06:13 AM
Modify CC Field Kamek Outlook and VBA 3 July 6th 06 10:00 PM


All times are GMT +1. The time now is 03:38 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.