Link to home
Start Free TrialLog in
Avatar of NobbyGee
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.Application")
Set myItem = myOlApp.ActiveInspector.CurrentItem
Set myAttachments = myItem.Attachments
myAttachments.Item(1).SaveAsFile "C:\" & _
    myAttachments.Item(1).DisplayName
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
Avatar of stefri
stefri
Flag of France image

just use the selection
Sub Saveattachements()
dim theSel as Outlook.Selection

set theSel = application.activeexplorer.selection
if theSel.count = 0 then

else
Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.ActiveInspector.CurrentItem
Set myAttachments = myItem.Attachments
myAttachments.Item(1).SaveAsFile "C:\" & _
    myAttachments.Item(1).DisplayName
myItem.Close
    End Sub
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
Avatar of NobbyGee
NobbyGee

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.
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
Avatar of stefri
stefri
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is brilliant thanks for all your help!!