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 
'__________________________________________________
'__________________________________________________

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: