outlook 2010 script

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
MrMayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David LeeCommented:
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
biaselectronicsCommented:
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
MrMayAuthor Commented:
Bluedevilfan.... what is tbe "4" for in the code?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

David Johnson, CD, MVPOwnerCommented:
he is checking the right 4 characters match
.pdf
1234
0
David LeeCommented:
@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
MrMayAuthor Commented:
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
David LeeCommented:
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
MrMayAuthor Commented:
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
MrMayAuthor Commented:
have a look at the attached bluedevlifan... please & thank you.
... needs to go to the gmailemails folder....
capture.JPG
0
David LeeCommented:
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
MrMayAuthor Commented:
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
David LeeCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MrMayAuthor Commented:
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
MrMayAuthor Commented:
see attached... this is what I have at this point.
capture.JPG
0
David LeeCommented:
Do you see the Developer tab on the ribbon?
0
MrMayAuthor Commented:
I got it to work.... THANK YOU, THANK YOU ,THANK YOU....   :-)   this is perfect....
0
David LeeCommented:
You're welcome.  Happy to help.
0
MrMayAuthor Commented:
I've requested that this question be deleted for the following reason:

task at hand was cancelled.
0
David Johnson, CD, MVPOwnerCommented:
the asked question was answered with a thanks now he wants it deleted?
0
David LeeCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.