I am using the Dir() function to assess all the files in a folder and open the latest revision automatically. The latest revision of the file is always the latest in the alphabet (e.g. file 1234AA is ignored, and file 1234AB should be opened).
I've been using a macro for a while that I'm sure used to be okay, but that now seems to be opening up older revisions some of the time (not all the time). For example, I have a file with the following revisions 1234BB.pdf, 1234BC.pdf, 1234BD.pdf, 1234BE.pdf and it's opening 1234BC.pdf. If I create a dummy file 1234BF.txt, it then finds this one! Not sure why it can't 'see' some of the original files. Other times, the macros opens the latest revision, even if there are 30 versions.
The function below is what I use to get the path of the latest revision, where I would pass "1234" as dwgNo:
Function getFullFile(dwgNo As String) As String
Dim xFname$, InitialFoldr$, xFname2Open$
InitialFoldr$ = "\\xxx\xxx\Design\" '<<< Startup folder to begin searching from
xFname$ = Dir(InitialFoldr$, 7)
xFname2Open$ = dwgNo
Do While xFname$ <> ""
If xFname$ <> "Thumbs.db" And InStr(xFname$, dwgNo) > 0 Then
xFname2Open$ = xFname$
End If
xFname$ = Dir
Loop
getFullFile = InitialFoldr$ & xFname2Open$
End Function
I've tried to simplify the statements, tried to see if reordering the files by sorting and that hasn't worked.
I think it might be to do with the "created date" - sometimes older files are automatically regenerated by ePDM and in the example above, the "created date" of "1234BC.pdf" is later than the date of 1234BE.pdf. I'm sure this is what's driving the wrong file being accessed but not sure how to make the dir() cycle through by "Name" not "date created"?
Any help appreciated!