Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-15-2014, 02:07 AM
gmayor's Avatar
gmayor gmayor is offline VBA Support required (trying to automatically filter PDFs from emails in specific folders. Windows 7 64bit VBA Support required (trying to automatically filter PDFs from emails in specific folders. Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

I am inclined to think that this may not be the ideal approach. I can see why you want the most recent version, but I wonder whether the modified date will reflect the version you want. Such dates are notoriously fickle.



If I was doing it for myself, I would save numbered versions of all the files. That way you have all versions. You can then establish for certain which is the most recent.

It is either that or trust that the date the message was sent makes the file the most recent, in which case, as you are processing the messages as they arrive, you can just overwrite the previous version as they arrive.

There would however be the possibility that different files have the same name, so that takes us back to my original suggestion and to that end I would suggest the following:

Code:
Option Explicit

Sub SaveAttachmentsToDisk(Item As Outlook.MailItem)
Dim olkAttachment As Outlook.Attachment
Dim objFSO As Object
Dim strRootFolderPath As String
Dim strFilename As String

    'Change the following path to match your environment
    strRootFolderPath = "Y:\"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If Item.Attachments.Count > 0 Then
        For Each olkAttachment In Item.Attachments
            If objFSO.GetExtensionName(LCase(olkAttachment.Filename)) = "pdf" Then
                strFilename = strRootFolderPath & olkAttachment.Filename
                strFilename = FileNameUnique(strRootFolderPath, strFilename, "pdf")
                olkAttachment.SaveAsFile strFilename
            End If
        Next
    End If
    Set objFSO = Nothing
    Set olkAttachment = Nothing
End Sub

Private Function FileNameUnique(strPath As String, _
                               strFilename As String, _
                               strExtension As String) As String
Dim lngF As Long
Dim lngName As Long
    lngF = 1
    lngName = Len(strFilename) - (Len(strExtension) + 1)
    strFilename = Left(strFilename, lngName)
    Do While FileExists(strFilename & Chr(46) & strExtension) = True
        strFilename = Left(strFilename, lngName) & "(" & lngF & ")"
        lngF = lngF + 1
    Loop
    FileNameUnique = strFilename & Chr(46) & strExtension
End Function

Private Function FileExists(ByVal Filename As String) As Boolean
Dim lngAttr As Long
    On Error GoTo NoFile
    lngAttr = GetAttr(Filename)
    If (lngAttr And vbDirectory) <> vbDirectory Then
        FileExists = True
    End If
NoFile:
    Exit Function
End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sent emails end up in both Sent and Draft folders Begadoc Outlook 2 11-26-2013 02:42 PM
How to set style automatically for specific texts ragesz Word 2 07-25-2013 07:08 AM
5 Folders Automatically Created under Inbox of Outlook 2007 yashwant Outlook 0 04-26-2012 12:28 AM
VBA Support required (trying to automatically filter PDFs from emails in specific folders. Merging outlook folders and emails rudihorvath Outlook 1 03-16-2012 07:03 AM
Current view filter applies automatically lumisy Outlook 3 03-25-2011 05:44 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:39 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft