+ Reply to Thread
Results 1 to 6 of 6

VBA code to open latest file

Hybrid View

  1. #1
    Registered User
    Join Date
    02-01-2016
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2007
    Posts
    70

    VBA code to open latest file

    Hi,

    I am trying to open most latest & greatest .pdf file from a folder that has lets say, 123456AA.pdf, 123456AB.pdf, 123456AC.pdf, 123456AD,......123456BW.pdf

    I need to open the 123456BW.pdf. I have VBA code as follows.



    Sub OpenPDFdoc()

    Dim Folder As String

    Folder = Left(ActiveCell.Value, 3)

    On Error GoTo 1

    ActiveWorkbook.FollowHyperlink "U:\Engineering\Design\DWG\" & Folder & "\" & ActiveCell.Value & ".pdf", NewWindow:=True

    Exit Sub

    1: MsgBox "File Cannot Be Opened"

    End Sub

    With this code I can open a specific folder. But I want to open the latest file and open it.

    Attached excel file shows D7 & D8 cell has "151604EV.pdf" which are the latest revision. I want vba to find EV version of 15604 file.

    Any help on vba would be highly appreciated.

    Thanks.

    Mashiul
    Attached Files Attached Files
    Last edited by mashiulalam; 06-09-2016 at 09:26 AM.

  2. #2
    Forum Contributor
    Join Date
    01-20-2012
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    186

    Re: VBA code to open latest file

    Hi Mashiul

    Try this:
    Option Explicit
    
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
     
    Sub OpenGreatestFile()
         
        Dim xRow As Long
        Dim xDirect$, xFname$, InitialFoldr$, xFname2Open$
         
        InitialFoldr$ = "U:\Engineering\Design\DWG\" & Left(ActiveCell.Value, 3) & "\" '<<< Startup folder to begin searching from
         
                xFname$ = Dir(InitialFoldr$, 7)
                xFname2Open$ = xFname$
                Do While xFname$ <> ""
                    If xFname$ > xFname2Open$ And xFname$ <> "Thumbs.db" Then
                        xFname2Open$ = xFname$
                    End If
                    xFname$ = Dir
                Loop
            
        ShellExecute 0, "Open", InitialFoldr$ & xFname2Open$, "", "", vbNormalNoFocus
            
    End Sub
    Regards,
    Rick

  3. #3
    Registered User
    Join Date
    02-01-2016
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2007
    Posts
    70

    Re: VBA code to open latest file

    Hi Rick,

    Thank you very much for your code.

    I am not an expert of VBA. Let me give you the steps what I really want,

    1. Read the intial d7 value as let say, 151604
    2. Search greatest version of pdf file as 151604EV.pdf (initial value is 151604AA.pdf) at the startup folder at U:\Engineering\Design\DWG\151\
    3. Open 151604EV.pdf

    I ran your code and I am getting error.

    Any further help would be highly appreciated.

    Thanks.

    Mashiul

  4. #4
    Forum Contributor
    Join Date
    01-20-2012
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    186

    Re: VBA code to open latest file

    Hi Mashiul,

    Then try this:
    Option Explicit
    
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
     
    Sub OpenGreatestFile()
         
        Dim xRow As Long
        Dim xDirect$, xFname$, InitialFoldr$, xFname2Open$
         
        InitialFoldr$ = "U:\Engineering\Design\DWG\151\" '<<< Startup folder to begin searching from
         
                xFname$ = Dir(InitialFoldr$, 7)
                xFname2Open$ = Range("D7").Value
                Do While xFname$ <> ""
                    If xFname$ > xFname2Open$ _
                       And xFname$ <> "Thumbs.db" _
                       And InStr(xFname$, Range("D7").Value) > 0 _
                    Then
                        xFname2Open$ = xFname$
                    End If
                    xFname$ = Dir
                Loop
            
        ShellExecute 0, "Open", InitialFoldr$ & xFname2Open$, "", "", vbNormalNoFocus
            
    End Sub
    Regards,
    Rick

  5. #5
    Registered User
    Join Date
    02-01-2016
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2007
    Posts
    70

    Re: VBA code to open latest file

    Hi Rick,

    It's working perfectly. That's what I really need.

    Thanks very much & appreciated.

    Mashiul

  6. #6
    Registered User
    Join Date
    02-01-2016
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2007
    Posts
    70

    Re: VBA code to open latest file

    Hi Rick,

    Can you please help me with adding one more command? I would like to save the opened latest pdf file to a folder let say, "C:\pdfs".

    Your kind help would be highly appreciated.

    Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA -> Open Latest Version of a File [After Read-Only Save]
    By BravesPiano5 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-26-2015, 05:53 PM
  2. [SOLVED] Open File With Latest Date
    By AstToTheRegionalMGR in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-12-2015, 12:39 PM
  3. Replies: 9
    Last Post: 11-28-2013, 05:20 PM
  4. Open the 2nd latest file in a folder?
    By guydixon in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-27-2013, 01:37 PM
  5. Macro to open latest file within a folder
    By Roop in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2012, 02:56 PM
  6. Open the latest file to copy and paste data in another file
    By mmf in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-07-2008, 10:41 AM
  7. macro to open latest file
    By tsgol in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-25-2005, 08:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1