Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 21st, 2004, 10:21 AM
Registered User
 
Join Date: Apr 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need Help Building a Logfile

I'm building a log file so I can track who acceses a spreadsheetfor my company. I have the following code setup to capture the login and whether they are opening or closing the file. I would like to add a date/time to the log as well. Any suggestions? Thanks for your help!
----------------------------------------------------------------------
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Open "W:\RSSC\Getronics\backup\log.txt" For Append As #1
Print #1, Environ("username") & " Closed Getronics SLA-Billable"
Close #1

On Error GoTo errhandle

Dim yesterdayfile As String
Dim todayfile As String


yesterdayfile = "W:\RSSC\Getronics\backup\Getronics SLA-Billable" & Weekday(Date - 3) & ".xls"
todayfile = "W:\RSSC\Getronics\backup\Getronics SLA-Billable" & Weekday(Date) & ".xls"


question = MsgBox("Would you like to save the Getronics SLA-Billable? This will save any and all changes made during this session. ", vbYesNo, "Warning")

If question = vbYes Then

Save
If FileExist(yesterdayfile) Then
    Kill yesterdayfile
End If

srcfile = "W:\RSSC\Getronics\Getronics SLA-Billable.XLS"
tgtfile = "W:\RSSC\Getronics\backup\Getronics SLA-Billable.XLS"

If FileExist(todayfile) Then
    Kill todayfile
    SaveAs todayfile
Else
SaveAs todayfile
End If

Exit Sub

Else

MsgBox "Getronics SLA-Billable not saved.", vbCritical, "Error"

Exit Sub
End If
errhandle:

MsgBox "Error Saving backup of spreadsheet.", vbCritical, "Error"


End Sub

Public Function FileExist(asPath As String) As Boolean

    If UCase(Dir(asPath)) = UCase(TrimPath(asPath)) Then
      FileExist = True
    Else
      FileExist = False
    End If

End Function

Public Function TrimPath(ByVal asPath As String) As String

    If Len(asPath) = 0 Then Exit Function
    Dim x As Integer

    Do
        x = InStr(asPath, "\")
        If x = 0 Then Exit Do
        asPath = Right(asPath, Len(asPath) - x)
    Loop
    TrimPath = asPath

End Function

Private Sub Workbook_Open()
Open "W:\RSSC\Getronics\backup\log.txt" For Append As #1
Print #1, Environ("username") & " Opened Getronics SLA-Billable"

Close #1
End Sub

 
Old May 5th, 2004, 10:56 AM
Registered User
 
Join Date: Apr 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok I kinda found a solution to my problem. I can get the date to display but the time shows up as 00:00 rather than current time. Here's the code:
----------------------------------------------------------
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Open "W:\RSSC\Getronics\backup\log.txt" For Append As #1
Print #1, Environ("username") & " Closed Getronics SLA-Billable " & Format(Date, "yyyy-mm-dd hh:mm")
Close #1

On Error GoTo errhandle

Dim yesterdayfile As String
Dim todayfile As String


yesterdayfile = "W:\RSSC\Getronics\backup\Getronics SLA-Billable" & Weekday(Date - 3) & ".xls"
todayfile = "W:\RSSC\Getronics\backup\Getronics SLA-Billable" & Weekday(Date) & ".xls"


question = MsgBox("Would you like to save the Getronics SLA-Billable? This will save any and all changes made during this session. ", vbYesNo, "Warning")

If question = vbYes Then

Save
If FileExist(yesterdayfile) Then
    Kill yesterdayfile
End If

srcfile = "W:\RSSC\Getronics\Getronics SLA-Billable.XLS"
tgtfile = "W:\RSSC\Getronics\backup\Getronics SLA-Billable.XLS"

If FileExist(todayfile) Then
    Kill todayfile
    SaveAs todayfile
Else
SaveAs todayfile
End If

Exit Sub

Else

MsgBox "Getronics SLA-Billable not saved.", vbCritical, "Error"

Exit Sub
End If
errhandle:

MsgBox "Error Saving backup of spreadsheet.", vbCritical, "Error"


End Sub

Public Function FileExist(asPath As String) As Boolean

    If UCase(Dir(asPath)) = UCase(TrimPath(asPath)) Then
      FileExist = True
    Else
      FileExist = False
    End If

End Function

Public Function TrimPath(ByVal asPath As String) As String

    If Len(asPath) = 0 Then Exit Function
    Dim x As Integer

    Do
        x = InStr(asPath, "\")
        If x = 0 Then Exit Do
        asPath = Right(asPath, Len(asPath) - x)
    Loop
    TrimPath = asPath

End Function

Private Sub Workbook_Open()
Open "W:\RSSC\Getronics\backup\log.txt" For Append As #1
Print #1, Environ("username") & " Opened Getronics SLA-Billable" & Format(Date, "yyyy-mm-dd hh:mm")

Close #1
End Sub






Similar Threads
Thread Thread Starter Forum Replies Last Post
Query building anjurenjith SQL Language 0 August 2nd, 2007 05:07 AM
create server side logfile from javascript senthilkumar Javascript 1 March 22nd, 2006 02:50 PM
FSO Logfile Problem acdsky Classic ASP Basics 2 April 11th, 2004 10:27 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.