?
Solved

outlook 2010 script

Posted on 2014-10-21
22
Medium Priority
?
270 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
[X]
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
  • 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 82

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 1000 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 1000 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 82

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This article describes how to import Lotus Notes Contacts into Outlook 2016, 2013, 2010 and 2007 etc. with a few manual steps. You can easily export and migrate Lotus Notes contacts into Microsoft Outlook without having to use any third party tools.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

765 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