Solved

How to save an Outlook 2010 email with an excel attachment to a folder?

Posted on 2014-01-21
8
407 Views
Last Modified: 2014-02-04
When I get an email in Outlook 2010 with the subject contains the word "TEC" I need to save to a folder.
The VBA code to save an excel file to a folder will be set off by a outlook rule.
The path for the folder is...\\psf\Home\TEC Reports
Thanks,
Jimi
0
Comment
Question by:Jimi Sherman
  • 4
  • 4
8 Comments
 
LVL 39

Accepted Solution

by:
nutsch earned 500 total points
ID: 39799150
Something like this?

Sub SaveAttachment()
Dim sPath As String, sPathC As String
Dim lPos As Long
Dim MyFile As String
Dim sAmount As String, dAmount As Double
Dim colAttachments As Outlook.Attachments
Dim objAttachment As Outlook.attachment
Dim FolderPath As String

    Dim curmail As Outlook.MailItem, sCat As String
    Set curmail = GetCurrentItem()
      
    Set colAttachments = curmail .Attachments
    
    FolderPath = "\\psf\Home\TEC Reports"
    
For Each objAttachment In colAttachments
    
        MyFile = objAttachment.FileName
        sPath = FolderPath & "\" & MyFile
        objAttachment.SaveAsFile sPath

    Next
   
   curMail.Delete
   
    'Cleanup
    Set objAttachment = Nothing
    Set colAttachments = Nothing
    Set MyOlApplication = Nothing
    Set myOlSelection = Nothing
    Set mySelectedItem = Nothing

End Sub

' From http://www.outlookcode.com/codedetail.aspx?id=50
Function GetCurrentItem() As Object
    Dim objApp As Outlook.Application
         
    Set objApp = CreateObject("Outlook.Application")
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"
            Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
        Case "Inspector"
            Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
        Case Else
            ' anything else will result in an error, which is
            ' why we have the error handler above
    End Select
     
    Set objApp = Nothing
End Function

Open in new window


Thomas
0
 

Author Comment

by:Jimi Sherman
ID: 39799186
The code stops at...
objAttachment.SaveAsFile sPath

Jimi
0
 
LVL 39

Expert Comment

by:nutsch
ID: 39799910
Does the following path exist?  
\\psf\Home\TEC Reports
0
 

Author Comment

by:Jimi Sherman
ID: 39800424
Yes
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 39

Expert Comment

by:nutsch
ID: 39800474
Any error message you're getting?
0
 

Author Comment

by:Jimi Sherman
ID: 39800531
No error message it just stops with the debug box opens.
I got it to work when I changed the path to...."C:\Users\Jimi\Documents"
So if you want i can close the question and issues you the points of courses?

Jimi
0
 
LVL 39

Expert Comment

by:nutsch
ID: 39800694
Can you map a drive to your destination? Does it work then?
0
 

Author Comment

by:Jimi Sherman
ID: 39800730
Yes it works then.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Use email signature images to promote corporate certifications and industry awards.
Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now