VBAlearner2010
asked on
VBA Doubt
How to make the following code work in Office 2010. Please edit the code with the suitable replacement for Application.Filesearch
Function LastFileSaved(sFileDir As String, DebutNomFichier As String, Optional sType As String = "*.*") As String
Dim fs
Set fs = Application.FileSearch
With fs
.LookIn = sFileDir
.Filename = DebutNomFichier
If .Execute(SortBy:=msoSortByLastModified, _
SortOrder:=msoSortOrderDescending) > 0 Then
LastFileSaved = .FoundFiles(1)
Else
MsgBox "There were no files found in:" & vbLf & _
sFileDir & vbLf & _
"Type: " & sType
LastFileSaved = vbNullString
End If
End With
End Function
Function FileExist(Chemin, NomFichier) As Boolean
' Ne marche plus avec Excel 2007
Set fs = Application.FileSearch
With fs
Resultat = False
.LookIn = Chemin
.Filename = NomFichier
If .Execute > 0 Then FileExist = True Else FileExist = False
End With
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@mbizup & akoster:: Is this the replacement for both the functions? Last file saved and File exists?
Will it work if i replace both these functions with the one function?
Please advise since I am very new and not getting hold in coding yet. It will be very kind if you can explain me what the line does..
Will it work if i replace both these functions with the one function?
Please advise since I am very new and not getting hold in coding yet. It will be very kind if you can explain me what the line does..
No - Just for FileExist.
These are really two seperate questions...
These are really two seperate questions...
Both of our code blocks use the Dir function to check for a file in a directory. Mine checks for the existence of a backslash character at the end of the folder (chemin) if it is not there, it adds it. This is why it is a little more verbose, but our posts basically do the same thing.
ASKER
Ok i will post last file saved as a seperate question. Thanks for this :)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@akoster: I get an error User defined or type defined error on the line "Set fso = New FileSystemObject"
Should i add any reference because when i hit space after AS when declaring i dont see FilesystemObject in the list.
Should i add any reference because when i hit space after AS when declaring i dont see FilesystemObject in the list.
ASKER
Kindly let me know asap... I will have to deliver this app today.
ASKER
I added Microsoft scripting Runtime to reference.. And it compiled.. Thanks for the help.
ASKER
Thanks for the help
Open in new window
this works in office 2007