Create LogFile for Workbook Open

- Selva V Pasupathy, HSBC Global Resourcing, Hyderabad

The following code will creqate log entry each time a workbook is opened.

' Original Source:	johnske, www.vbaexpress.com
'__________________________________________________
'	CODE FOR MODULE1
'__________________________________________________

Option Explicit 

Public Function LogInformation(LogMessage$)
    On Error Goto MakeFolder
Entry:
  Open Thisworkbook.path & "\MyLogFiles\" _
	& Left(ThisWorkbook.Name, _
  Len(ThisWorkbook.Name) - 4) & _
  " Log.Log" For Append As #1
  Print #1, LogMessage
  Close #1
  Exit Function
MakeFolder:
  MkDir Thisworkbook.path & "\MyLogFiles\"
  Resume Entry
End Function
'__________________________________________________
'__________________________________________________

'__________________________________________________
'	CODE FOR THISWORKBOOK MODULE
'__________________________________________________

Option Explicit 

Private Sub Workbook_Open()
  LogInformation "Opened by " & _
         Application.UserName & _
         " " & Format(Now, "dd mmm yyyy hh:mm:ss")
End Sub
'__________________________________________________
'__________________________________________________

Leave a Comment

You must be logged in to post a comment.