Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

outlook 2010 script

Posted on 2014-10-21
22
261 Views
Last Modified: 2015-02-05
Hello,
i need some help... i need to create a rule in outlook 2010 that will go through a folder in the cabinet and take attachments out of those incoming emails and copy the attachments to a folder in my c drive. I found a bunch of scripts online but I can't seem to get it to work properly.

So if i have emails coming in from test@gmail.com every 5 mins and each email has a pdf attachment... i need to save that pdf attachment to c:\folder  
can someone provide the solution... please and thank you
0
Comment
Question by:MrMay
  • 9
  • 8
  • 2
  • +1
22 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 40396050
Hi, MrMay.

This should do it.  I'm assuming that you know how to add the code to Outlook and create the rule that calls the macro.

Sub SavePDFAttachments(Item As Outlook.MailItem)
    'On the following line, edit the path of the folder the macro will save the attachments to
    Const SAVE_TO_FOLDER = "C:\users\david\documents\"
    Dim olkAtt As Outlook.Attachment
    For Each olkAtt In Item.Attachments
        If Right(LCase(olkAtt.FileName), 4) = ".pdf" Then
            olkAtt.SaveAsFile SAVE_TO_FOLDER & olkAtt.FileName
        End If
    Next
    Set olkAtt = Nothing
End Sub

Open in new window

0
 
LVL 3

Expert Comment

by:biaselectronics
ID: 40396053
I would create a rule that would trigger when a new message from test@gmail.com comes into the inbox and then run the script.
You would need to be careful about duplicate names, so assuming an average of 5 minutes between emails, you can stamp their names with the time in seconds, something like this:

Sub MoveFile(Item As Outlook.MailItem)

On Error GoTo errHandler

Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat

dateFormat = Format(Now, "mmdd H-mm-ss")
saveFolder = "C:\yourfolder"

For Each objAtt In Item.Attachments
 objAtt.SaveAsFile saveFolder & "\" & dateFormat & "-" & objAtt.DisplayName
 Set objAtt = Nothing
Next


errHandler:
    MsgBox "Error " & Err.Number & ": " & Err.Description & " in " & VBE.ActiveCodePane.CodeModule, vbOKOnly, "Error"

End Sub
0
 

Author Comment

by:MrMay
ID: 40396136
Bluedevilfan.... what is tbe "4" for in the code?
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 80

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 250 total points
ID: 40396148
he is checking the right 4 characters match
.pdf
1234
0
 
LVL 76

Expert Comment

by:David Lee
ID: 40396530
@MrMay - As @David Johnson has already noted, the code is checking the last 4 characters of the file name to find just the PDF files.  If you want to save PDF files with a certain name, then that's possible too with a slight modification of the code.
0
 

Author Comment

by:MrMay
ID: 40396724
BlueDevilFan this is perfect.... one other question.... I was thinking that i can just create a rule to move the emails that had pdf attachments to another cabinet folder but instead the rule moves all the emails to a new folder.
Let me explain; when I get emails from test@gmail.com some have pdf attachments and others don't. I want to copy the pdf attachments to another folder (which your code does.. thank you) and then take those emails that had the attachment and still move them to another cabinet. So move them from my inbox to a "gmail" folder in my cabinet in outlook.
By using a rule it moves all the emails from test@gmail.com even the ones without any attachments.  can i add some code to your script that will do this for me?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 40396988
Yes, that's possible.  Replace the first version of the code with this version.  You'll need to supply the path to the Outlook folder you want to move the messages that had a PDF attachment to.  Note that this an Outlook folder path, not the path to a folder in the file system.

Sub SavePDFAttachments(Item As Outlook.MailItem)
    'On the following line, edit the path of the folder the macro will save the attachments to
    Const SAVE_TO_FOLDER = "C:\users\david\documents\"
    'On the next line, edit the path to the Outlook folder you want to archive messages to
    Const OUTLOOK_ARCHIVE_FOLDER = "Some_Folder_Path"
    Dim olkAtt As Outlook.Attachment, bolPDF As Boolean
    For Each olkAtt In Item.Attachments
        If Right(LCase(olkAtt.Filename), 4) = ".pdf" Then
            olkAtt.SaveAsFile SAVE_TO_FOLDER & olkAtt.Filename
            bolPDF = True
        End If
    Next
    If bolPDF Then
        Item.Move OpenOutlookFolder(OUTLOOK_ARCHIVE_FOLDER)
    End If
    Set olkAtt = Nothing
End Sub

Private Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
    ' Purpose: Opens an Outlook folder from a folder path.
    ' Written: 4/24/2009
    ' Author:  David Lee
    ' Outlook: All versions
    Dim arrFolders As Variant, _
        varFolder As Variant, _
        bolBeyondRoot As Boolean
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = Outlook.Session.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
        Next
    End If
    On Error GoTo 0
End Function

Open in new window

0
 

Author Comment

by:MrMay
ID: 40397010
bluedevilfan... i adjust this line
Item.Move OpenOutlookFolder(OUTLOOK_ARCHIVE_FOLDER)  

so if my outlook cabinet folder was called "gmailemails", i would make this line look like this
Item.Move OpenOutlookFolder(gmailemails)   ?
0
 

Author Comment

by:MrMay
ID: 40397031
have a look at the attached bluedevlifan... please & thank you.
... needs to go to the gmailemails folder....
capture.JPG
0
 
LVL 76

Expert Comment

by:David Lee
ID: 40397106
No, that's not right.  Don't change this line

Item.Move OpenOutlookFolder(OUTLOOK_ARCHIVE_FOLDER)   

Open in new window


at all.  Change this line

Const OUTLOOK_ARCHIVE_FOLDER = "Some_Folder_Path"

Open in new window


to

Const OUTLOOK_ARCHIVE_FOLDER = "cabinet\gmailemails"

Open in new window

0
 

Author Comment

by:MrMay
ID: 40397283
its not working.... i changed that line of code to Const OUTLOOK_ARCHIVE_FOLDER = "cabinet\gmailemails" .
The pdfs get dumped to my test folder but the actual email does not get moved.
Could the syntax be possibly different?
0
 
LVL 76

Accepted Solution

by:
David Lee earned 250 total points
ID: 40397510
The screenshot is clipped and I may have missed a level in the folder hierarchy.  Add this code to the code you already have.  Once you've done that, select the "gmailemails" folder and run the DisplayFolderPath macro.  It will display a dialog-box showing the path to the selected folder.  Copy the path from that dialog-box and use it on the

Const OUTLOOK_ARCHIVE_FOLDER = "cabinet\gmailemails"

line.

Public Sub DisplayFolderPath()
    MsgBox "The path to the currently selected folder is " & vbCrLf & vbCrLf & Application.ActiveExplorer.CurrentFolder.FolderPath, vbInformation + vbOKOnly, "Display Folder Path"
End Sub

Open in new window

0
 

Author Comment

by:MrMay
ID: 40397552
I'm sorry bluedevilfan... i don't understand what you want me to do.
I copied the code that you posted above, but how do I run it?
please provide steps.
0
 

Author Comment

by:MrMay
ID: 40397557
see attached... this is what I have at this point.
capture.JPG
0
 
LVL 76

Expert Comment

by:David Lee
ID: 40397618
Do you see the Developer tab on the ribbon?
0
 

Author Comment

by:MrMay
ID: 40397776
I got it to work.... THANK YOU, THANK YOU ,THANK YOU....   :-)   this is perfect....
0
 
LVL 76

Expert Comment

by:David Lee
ID: 40398457
You're welcome.  Happy to help.
0
 

Author Comment

by:MrMay
ID: 40571377
I've requested that this question be deleted for the following reason:

task at hand was cancelled.
0
 
LVL 80

Expert Comment

by:David Johnson, CD, MVP
ID: 40571378
the asked question was answered with a thanks now he wants it deleted?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 40572412
I also object.  An answer was accepted.  It's a bit like hiring a painter to paint you're house and then after the work is done deciding that you didn't want to paint your house after all.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
Many people use more than one email account and so it becomes difficult for them to manage them when they use separate accounts,  so, in this article, I have shared an easy way to add Other Mail Accounts in your Google Inbox. It helps to combine all…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

807 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