Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Excel File System Object

I'm trying to develop a bit of VBA that will check the date modified section on the file. I have found a bit of code online that uses the FileSystemObject to do this, but I run into a "Type Mismatch" error in VBA and was hoping someone could help..

Sub test()
Dim FileLastModified As Variant
MsgBox FileLastModified("S:\FILEPATHISHERE.xls")
End Sub

(naturally i have entered the actual filepath there!)

Function FileLastModified(strFullFileName As String)
Dim fs As Object, f As Object, s As String

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(strFullFileName)

s = UCase(strFullFileName) & vbCrLf
s = s & "Last Modified: " & f.DateLastModified
FileLastModified = s

Set fs = Nothing: Set f = Nothing
End Function

I have just added the Microsoft Scripting Runtime Reference, but this is still not working. Any ideas? Am I missing other required references?

Thanks in advance Alex

like image 248
alexei7 Avatar asked May 01 '26 02:05

alexei7


1 Answers

The problem is that you declare a variable FileLastModified in your test procedure, that has the same name as the function you want to call. If you delete that line it should work:

Sub test()
    MsgBox FileLastModified("S:\FILEPATHISHERE.xls")
End Sub
like image 81
assylias Avatar answered May 02 '26 23:05

assylias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!