View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
isabelle isabelle is offline
external usenet poster
 
Posts: 587
Default Check for file open/closed status

hi Mick,

there is a possibility that you can create a log file.
the following code creates the text file "C:\xlslog.txt"
you need to copy this code on ThisWorkbook

Private Sub Workbook_open()
Ecritinfos ("ouvre")
TextStreamTest
End Sub

Sub Ecritinfos(data)
Open "c:\xlslog.txt" For Append As #1
Print #1, Format(Date, "dd/mm/yy ") & Format(Time, "hh:nn:ss") & " " & data & " " & ThisWorkbook.Name & " " & Application.UserName
Close #1
End Sub

Sub TextStreamTest()
Const ForReading = 1
Const TristateUseDefault = -2
Dim fs, f, ts, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile("c:\xlslog.txt")
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
s = Right(ts.ReadAll, 70)
MsgBox s
ts.Close
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Ecritinfos ("Ferme")
End Sub



you can check the result with the following function after saving the file, closing it and reopen it.

=testRead("c:\xlslog.txt")

Function testRead(fichier As String)
Const ForReading = 1
Const TristateUseDefault = -2
Dim fs, f, ts
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(fichier)
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
testRead = Right(ts.ReadAll, 70)
ts.Close
End Function


--
isabelle