Solved

outlook 2010 script

Posted on 2014-10-21
22
248 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
Comment Utility
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
Comment Utility
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
Comment Utility
Bluedevilfan.... what is tbe "4" for in the code?
0
 
LVL 78

Assisted Solution

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

Expert Comment

by:David Lee
Comment Utility
@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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:MrMay
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
see attached... this is what I have at this point.
capture.JPG
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Do you see the Developer tab on the ribbon?
0
 

Author Comment

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

Expert Comment

by:David Lee
Comment Utility
You're welcome.  Happy to help.
0
 

Author Comment

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

task at hand was cancelled.
0
 
LVL 78

Expert Comment

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

Expert Comment

by:David Lee
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
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 video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

16 Experts available now in Live!

Get 1:1 Help Now