[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need help on Writing Macros to open email, save attachment and then close email in OL2002

Posted on 2004-11-16
6
Medium Priority
?
482 Views
Last Modified: 2010-04-08
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
0
Comment
Question by:NobbyGee
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:stefri
ID: 12607828
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
0
 
LVL 13

Expert Comment

by:stefri
ID: 12607971
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
0
 

Author Comment

by:NobbyGee
ID: 12613053
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:NobbyGee
ID: 12613251
Another quick question can you rename your Macro Button that i have placed on my Standard toolbar as it reads Doc Image.save attachments?
0
 
LVL 13

Accepted Solution

by:
stefri earned 2000 total points
ID: 12617709
You are right, itm.close is not necessary as the item is not opened/modified (my fault, customizing an existing code), so comment it out
Rename the macro whatever you want as long as the button references it
You can also create/assign an icon to the macro button

Stefri
0
 

Author Comment

by:NobbyGee
ID: 12623116
That is brilliant thanks for all your help!!
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article lists the top 5 trialware OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their Exchange server is no longer available or other critical issues with Exchange server or impo…
Changing a few Outlook Options can help keep you organized!
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Suggested Courses

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question