A Word VBA macro to get username and record the date and time the document was opened and closed.
Private Sub Document_Open() -- Will run the macro if the document is open
Private Sub Document_Close() -- Will run the macro if the document is close
To run the macro, create a text file on this location.
D:\DocMonitor\wordUserClose.txt
D:\DocMonitor\wordUserOpen.txt
Change the file name and the location to any path and any desired file name.
==============================
Private Sub Document_Open()
Dim xUserName As String
Dim xDate As String
xUserName = Application.UserName
Dim xfileSystem, xwriteStream
xDate = Now()
Set xfileSystem = CreateObject("Scripting.FileSystemObject")
'fs.CreateTextFile "D:\DocMonitor\wordUserOpen.txt"
Set xwriteStream = xfileSystem.OpenTextFile("D:\DocMonitor\wordUserOpen.txt", 8, 0)
xwriteStream.WriteLine vbCrLf
xwriteStream.WriteLine Open Date and Time
xwriteStream.WriteLine xDate
xwriteStream.WriteLine xUserName
xwriteStream.Close
Set xfileSystem = Nothing
Set xwriteStream = Nothing
End Sub
==============================
Private Sub Document_Close()
Dim xUserName As String
Dim xDate As String
xUserName = Application.UserName
Dim xfileSystem, xwriteStream
xDate = Now()
Set xfileSystem = CreateObject("Scripting.FileSystemObject")
'fs.CreateTextFile "D:\DocMonitor\wordUserClose.txt"
Set xwriteStream = xfileSystem.OpenTextFile("D:\DocMonitor\wordUserClosetxt", 8, 0)
xwriteStream.WriteLine vbCrLf
xwriteStream.WriteLine Open Date and Time
xwriteStream.WriteLine xDate
xwriteStream.WriteLine xUserName
xwriteStream.Close
Set xfileSystem = Nothing
Set xwriteStream = Nothing
End Sub
==============================
Click labels below for more VBA tips..
Comments
Post a Comment