NobbyGee
asked on
Need help on Writing Macros to open email, save attachment and then close email in OL2002
I hope someone can help as i am getting very frustrated. I am new to VBA and i need to write a macro which will open up an email, save the attachment to a folder in My C drive and then close the email and return me back to my Inbox. I have managed to do the middle part using the following code: Taken from the VBA help which i run afteri doubleclick on the email it then saves the attachment in my C Drive. Which is great.
Sub Saveattachements()
Set myOlApp = CreateObject("Outlook.Appl ication")
Set myItem = myOlApp.ActiveInspector.Cu rrentItem
Set myAttachments = myItem.Attachments
myAttachments.Item(1).Save AsFile "C:\" & _
myAttachments.Item(1).Disp layName
myItem.Close
End Sub
But what i need is to be able to do click on it so that it opens saves the attachment and then closes the email. Have you any ideas as i don't seem to be getting anywhere.
I hope i have explained it clearly.
Matt
Sub Saveattachements()
Set myOlApp = CreateObject("Outlook.Appl
Set myItem = myOlApp.ActiveInspector.Cu
Set myAttachments = myItem.Attachments
myAttachments.Item(1).Save
myAttachments.Item(1).Disp
myItem.Close
End Sub
But what i need is to be able to do click on it so that it opens saves the attachment and then closes the email. Have you any ideas as i don't seem to be getting anywhere.
I hope i have explained it clearly.
Matt
oops. wrong key....
just use the selection object from Application
Sub Saveattachements()
dim theSel as Outlook.Selection
set theSel = application.activeexplorer .selection
if theSel.count = 0 then
exit sub
else
for each itm in theSel
Set myAttachments = itm.Attachments
for each att in myAttachments
att.SaveAsFile "C:\" & att.DisplayName
next
set att = nothing
itm.close
next
set itm = nothing
set theSel = nothing
end if
End Sub
Select a mail or a group of mails, then select the macro from Tools/Macros/Run or create a button, assign SaveAttachments sub to the button
Tools/Macros/Secutiry must be set to Medium
Stefri
just use the selection object from Application
Sub Saveattachements()
dim theSel as Outlook.Selection
set theSel = application.activeexplorer
if theSel.count = 0 then
exit sub
else
for each itm in theSel
Set myAttachments = itm.Attachments
for each att in myAttachments
att.SaveAsFile "C:\" & att.DisplayName
next
set att = nothing
itm.close
next
set itm = nothing
set theSel = nothing
end if
End Sub
Select a mail or a group of mails, then select the macro from Tools/Macros/Run or create a button, assign SaveAttachments sub to the button
Tools/Macros/Secutiry must be set to Medium
Stefri
ASKER
That was perfect the only problem i had was that it did not like the itm.close - I crossed that out and it works. Before i accept your answer which i will and give you an A - Any reason why it did this and what was the itm.close - All i did was highlight the email - did not need to open it and it saved into C.
ASKER
Another quick question can you rename your Macro Button that i have placed on my Standard toolbar as it reads Doc Image.save attachments?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That is brilliant thanks for all your help!!
Sub Saveattachements()
dim theSel as Outlook.Selection
set theSel = application.activeexplorer
if theSel.count = 0 then
else
Set myOlApp = CreateObject("Outlook.Appl
Set myItem = myOlApp.ActiveInspector.Cu
Set myAttachments = myItem.Attachments
myAttachments.Item(1).Save
myAttachments.Item(1).Disp
myItem.Close
End Sub