VBA Function for Last Modified User using FSO

tatertot

New Member
Joined
Apr 10, 2016
Messages
31
I have the current VBA code for a function within Excel:

Code:
Sub test()
    Const strFullName As String = "C:\My Documents\nosuchbook.xls"
    
    If FileExists(strFullName) Then
        MsgBox FileLastModified(strFullName)
    Else
        MsgBox "Cannot find the file : " & vbNewLine & strFullName
    End If
    
End Sub
 
Private Function FileExists(fname) As Boolean
    Dim x As String
    x = Dir(fname)
    If x <> "" Then FileExists = True _
Else FileExists = False
End Function
 
Function FileLastModifiedDate(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 = f.datelastmodified
    FileLastModifiedDate = s
    
    Set fs = Nothing: Set f = Nothing
    
End Function

This function is perfectly providing the last saved date of a file link placed in the worksheet. I want this code to be modified to serve the exact same function as the code above but for the “Author” associated with the date it is providing.
Background: I am in charge of creating an overview form of a work instruction library for my department. When we need to review work instructions that are out of date, this workbook would allow the capability to see when the last modification date occurred and by who it was occurred by.
Any help on this would be AMAZING!!!!!
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Here's one way...

Code:
[COLOR=darkblue]Sub[/COLOR] test()

    [COLOR=darkblue]Dim[/COLOR] strLastSaveTime [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] strLastAuthor [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]

    [COLOR=darkblue]Const[/COLOR] strFullName [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] = "C:\My Documents\nosuchbook.xls"
    
    Application.ScreenUpdating = [COLOR=darkblue]False[/COLOR]
    
    [COLOR=darkblue]If[/COLOR] FileExists(strFullName) [COLOR=darkblue]Then[/COLOR]
        [COLOR=darkblue]Call[/COLOR] GetFileInfo(strFullName, strLastSaveTime, strLastAuthor)
        MsgBox "Last saved time:  " & strLastSaveTime & vbCrLf & "Last saved by:  " & strLastAuthor, vbInformation
    [COLOR=darkblue]Else[/COLOR]
        MsgBox "Cannot find the file : " & vbNewLine & strFullName
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    
    Application.ScreenUpdating = [COLOR=darkblue]True[/COLOR]
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Function[/COLOR] FileExists(fname) [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Boolean[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] x [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    x = Dir(fname)
    [COLOR=darkblue]If[/COLOR] x <> "" [COLOR=darkblue]Then[/COLOR] FileExists = True _
        [COLOR=darkblue]Else[/COLOR] FileExists = [COLOR=darkblue]False[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Function[/COLOR]

[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] GetFileInfo([COLOR=darkblue]ByVal[/COLOR] sFullFileName [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], [COLOR=darkblue]ByRef[/COLOR] sLastSaveTime [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], [COLOR=darkblue]ByRef[/COLOR] sLastAuthor [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR])
    [COLOR=darkblue]Dim[/COLOR] wb [COLOR=darkblue]As[/COLOR] Workbook
    [COLOR=darkblue]Set[/COLOR] wb = Workbooks.Open(Filename:=sFullFileName, ReadOnly:=True)
    [COLOR=darkblue]With[/COLOR] wb
        sLastSaveTime = wb.BuiltinDocumentProperties("Last Save Time")
        sLastAuthor = wb.BuiltinDocumentProperties("Last Author")
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    wb.Close SaveChanges:=[COLOR=darkblue]False[/COLOR]
    [COLOR=darkblue]Set[/COLOR] wb = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Hope this helps!
 
Upvote 0
Hi Domenic,

First off, thank you so much for your reply/support.
In reviewing your provided code, I had found that same type code format across other website forums. What I do not like is that you must open the file being referenced for each of the inserted links in cells.
Work Instruction
File Location
Last Date Modified
Last Date Accessed
Last Modified By
Month End Close
C:\”File Location”
3/1/2016
3/28/2017
John Doe

<tbody>
</tbody>

Above is the format of spreadsheet that contains the data. The concept of this workbook is to monitor changes of an entire work instruction library and each work instruction file within the library. What are department would like to monitor are the last 3 columns, [Last Date Modified], [Last Date Accessed], [Last Modified By].

I hope that this clarifies my goals and intentions behind this initial request. I look forward to your response. Thanks!!
 
Upvote 0
Actually, while the workbook is opened during the execution of the macro, it's done behind the scenes. So the user never sees it. Admittedly, depending on how many inserted links you do at a time, it could be a slow process. Have you tried it?
 
Last edited:
Upvote 0
Hi Domenic,

Excuse my incompetence but how do I run the provided code? I am not understanding how I can have this code reference a cell (i.e. C4) where the file address is and return the output of the "Last Author". I changed the "Private Sub" to a "function" and it returned "#VALUE".

Thanks!!
 
Upvote 0
Excuse my incompetence but how do I run the provided code?

Here's where to put the code...



  1. Open the workbook in which to store the code.
  2. Open the Visual Basic Editor (Alt+F11).
  3. Insert a standard module (Insert > Module).
  4. Copy/paste the code into the module.
  5. Return to Microsoft Excel (Alt+Q).
  6. Save the workbook.
Here's how to use it...


  1. Display the Macro dialog box (Alt+F8).
  2. Click/select the macro, which in this case is called "test".
  3. Click/select "Run".

I am not understanding how I can have this code reference a cell (i.e. C4) where the file address is...

Assuming that the sheet containing the path and filename (ie. C4) is the active sheet, try...

Code:
[COLOR=darkblue]Sub[/COLOR] test()

    [COLOR=darkblue]Dim[/COLOR] strFullName [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] strLastSaveTime [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] strLastAuthor [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]

    Application.ScreenUpdating = [COLOR=darkblue]False[/COLOR]
    
    strFullName = ActiveSheet.Range("C4").Value
    
    [COLOR=darkblue]If[/COLOR] FileExists(strFullName) [COLOR=darkblue]Then[/COLOR]
        [COLOR=darkblue]Call[/COLOR] GetFileInfo(strFullName, strLastSaveTime, strLastAuthor)
        MsgBox "Last saved time:  " & strLastSaveTime & vbCrLf & "Last saved by:  " & strLastAuthor, vbInformation
    [COLOR=darkblue]Else[/COLOR]
        MsgBox "Cannot find the file : " & vbNewLine & strFullName
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    
    Application.ScreenUpdating = [COLOR=darkblue]True[/COLOR]
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

I changed the "Private Sub" to a "function" and it returned "#VALUE".

Can I ask you why you want to change it to a function?
 
Upvote 0
I changed it to a function because my other 2 modules are functions that I am using to retrieve the last modified date & last accessed date. I am really confused why getting the last author data is so difficult to retrieve.

When I go to an office file saved on in my documents folder (i.e. C:\Users\JohnDoe\Worksheet1.xlsx) and I right click the file, I can select "Properties". Once the "Properties" window opens, I select "Details" tab. Under the section labeled "Origin" is a field labeled "Last saved by". That is what I am wanting to retrieve. I am wanting to not open the microsoft Office file, in this case Excel or Word, and retrieve data stored in the properties section of the files.

Currently I am able to retrieve the last saved date & last accessed date through a function code in VBA. Here is the code I am using for the function code of "last saved date":

Code:
Function FileLastModifiedDate(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 = f.datelastmodified
    FileLastModifiedDate = s
     
    Set fs = Nothing: Set f = Nothing
     
End Function

When i enter this function in the Excel Worksheet in cell "G6", I reference cell "C4" because it contains the live file hyperlink address of the work instruction file.

Does this help clarify my terrible methods of explaining my request? :)

Also, thank you very much for your already allocated time to this request Domenic!!
 
Upvote 0
The Shell object can be used to retrieve extended file properties without opening a workbook. However, while an "Authors" property is available, I don't see one for "Last saved by". Also, it seems that the "Authors" property returns an empty string for .xlsx and .xlsm files, but it seems to work for .xls files. So unfortunately I don't have anything else that I can offer you.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,109
Messages
6,128,883
Members
449,477
Latest member
panjongshing

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top