Link to home
Start Free TrialLog in
Avatar of MrMay
MrMay

asked on

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
Avatar of David Lee
David Lee
Flag of United States of America image

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

Avatar of biaselectronics
biaselectronics

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
Avatar of MrMay

ASKER

Bluedevilfan.... what is tbe "4" for in the code?
SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@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.
Avatar of MrMay

ASKER

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?
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

Avatar of MrMay

ASKER

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)   ?
Avatar of MrMay

ASKER

have a look at the attached bluedevlifan... please & thank you.
... needs to go to the gmailemails folder....
capture.JPG
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

Avatar of MrMay

ASKER

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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MrMay

ASKER

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.
Avatar of MrMay

ASKER

see attached... this is what I have at this point.
capture.JPG
Do you see the Developer tab on the ribbon?
Avatar of MrMay

ASKER

I got it to work.... THANK YOU, THANK YOU ,THANK YOU....   :-)   this is perfect....
You're welcome.  Happy to help.
Avatar of MrMay

ASKER

I've requested that this question be deleted for the following reason:

task at hand was cancelled.
the asked question was answered with a thanks now he wants it deleted?
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.