File Audit

ITTACD

New Member
Joined
Jan 4, 2008
Messages
1
I found the code below on the board some years ago and it works great but it needs to evolve to match my department’s needs. Is it possible to track the original file name when it was opened and its path, instead of typing that information into the origname variable?


Scenario
Joe opens "Standard Bidding Form 2008 Temp(03-17-08 Rates).xlt" and saves it as “Joe bidding.xls”, then Sally saves “Joe bidding.xls” it as “Sally Bidding.xls” Is it possible to see an audit trail for Sally’s file tracing through Joe’s file to the original "Standard Bidding Form 2008 Temp(03-17-08 Rates).xlt" and the directory in which the file is located?

Desired Outcome:
User Joe.csv file will show the following:
Directory and saved name: C:\Joe bidding.xls
Format(Date, "0"): Date
User name: Joe
Directory and source file: C:\Standard Bidding Form 2008 Temp(03-17-08 Rates).xlt

User Sally.csv file will show the following:
Directory and saved name: C:\Sally Bidding.xls
Format(Date, "0"): Date
User name: Sally
Directory and source file: C:\Joe bidding.xls



I have spent days working this and any help is appreciated, Thanks.


----------------------------------------------

This code will save a file per user and will track the saved file name, date, time, user name, and the original source file name when the workbook is closed.

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, _
nSize As Long) As Long



Sub Workbook_BeforeClose(cancel As Boolean)

origname = "Standard Bidding Form 2008 Temp(03-17-08 Rates).xlt" '************************* PUT THE FILENAME HERE
On Error GoTo carryon
' This bit gets the logon userid
Dim lpBuff As String * 25
Dim ret As Long, UserName As String
ret = GetUserName(lpBuff, 25)
UserName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)

f_name = ActiveWorkbook.Name
fname = UserName
fn = FreeFile
' the folder below must have universal read-write access, or the user cannot create the file
' will create a fle for each user - filename = xxx.csv, where xxx is the user's logon userid
Open "I:\dept\BusinessManagement_32300_FW\Matt\Excel\MISC\BB\" & fname & ".csv" For Append As fn
' will record every time the user closes the file
Dim x(6)
'add filename ,date, time and user to file
x(1) = f_name
x(2) = Format(Date, "0")
x(3) = Format(Time, "HH:MM:SS")
x(4) = UserName
x(5) = origname
Write #fn, x(1), x(2), x(3), x(4), x(5)
Close #fn

Exit Sub
carryon:
Close #fn
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I found the code below on the board some years ago and it works great but it needs to evolve to match my department’s needs. Is it possible to track the original file name when it was opened and its path, instead of typing that information into the origname variable?


Scenario
Joe opens "Standard Bidding Form 2008 Temp(03-17-08 Rates).xlt" and saves it as “Joe bidding.xls”, then Sally saves “Joe bidding.xls” it as “Sally Bidding.xls” Is it possible to see an audit trail for Sally’s file tracing through Joe’s file to the original "Standard Bidding Form 2008 Temp(03-17-08 Rates).xlt" and the directory in which the file is located?

Desired Outcome:
User Joe.csv file will show the following:
Directory and saved name: C:\Joe bidding.xls
Format(Date, "0"): Date
User name: Joe
Directory and source file: C:\Standard Bidding Form 2008 Temp(03-17-08 Rates).xlt

User Sally.csv file will show the following:
Directory and saved name: C:\Sally Bidding.xls
Format(Date, "0"): Date
User name: Sally
Directory and source file: C:\Joe bidding.xls



I have spent days working this and any help is appreciated, Thanks.


----------------------------------------------

This code will save a file per user and will track the saved file name, date, time, user name, and the original source file name when the workbook is closed.

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, _
nSize As Long) As Long



Sub Workbook_BeforeClose(cancel As Boolean)

origname = "Standard Bidding Form 2008 Temp(03-17-08 Rates).xlt" '************************* PUT THE FILENAME HERE
On Error GoTo carryon
' This bit gets the logon userid
Dim lpBuff As String * 25
Dim ret As Long, UserName As String
ret = GetUserName(lpBuff, 25)
UserName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)

f_name = ActiveWorkbook.Name
fname = UserName
fn = FreeFile
' the folder below must have universal read-write access, or the user cannot create the file
' will create a fle for each user - filename = xxx.csv, where xxx is the user's logon userid
Open "I:\dept\BusinessManagement_32300_FW\Matt\Excel\MISC\BB\" & fname & ".csv" For Append As fn
' will record every time the user closes the file
Dim x(6)
'add filename ,date, time and user to file
x(1) = f_name
x(2) = Format(Date, "0")
x(3) = Format(Time, "HH:MM:SS")
x(4) = UserName
x(5) = origname
Write #fn, x(1), x(2), x(3), x(4), x(5)
Close #fn

Exit Sub
carryon:
Close #fn
End Sub
Hi,

I'm interested in creating a log file very similar to what you've done here, but just different enough that it's giving me problems. I would like to attach the logging code to two different Internal_Click actions so that depending which one the user selects, a veriable can be set to the option the user clicked, and that option along with the date, time, and username, can be logged in the file. Currently, I'm wondering if maybe I don't have a reference included that I need for advapi32.dll. Do you know if a specific one needs to be included?

Thanks in advance!

Karen
 
Upvote 0

Forum statistics

Threads
1,225,204
Messages
6,183,569
Members
453,169
Latest member
Marlon18

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