這個代碼進入兩個工作簿
- 它使用
SheetActivate
事件到時間可持續寫入日誌您的主文件(name.xls的 電流片的在你的榜樣以上)至 a log.txt文件
- 「控制器」工作簿用於:
- 測試的主要文件是開放的,
- 如果它是那麼的只讀版本中打開(如果沒有實際的文件通常打開),並
- 文件日誌(這最後一片存儲,Windows登錄名&當前時間漸進式 - 也許矯枉過正)被訪問以設置最近的工作表。
注:
1.我只能通過我的主文件運行Excel的兩個獨立的情況下,在測試我的本地機器上此爲Excel不會讓相同的文件被打開兩次相同的實例)
2,而不是一個控制器工作簿我會建議使用vbscript從桌面快捷方式執行
改變這一行來設置文件路徑和名稱,以測試爲開放
StrFileName = "c:\temp\main.xlsm"
Code for document to be opened: ThisWorkbook module
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Open ThisWorkbook.Path & "\log.txt" For Append As #1
Print #1, Sh.Name & ";" & Environ("username") & ":" & Format(Now(), "dd-mmm-yy hh:mm")
Close #1
End Sub
Code for Controller workbook: Normal module
我已經更新了微軟網站的代碼來測試是否StrFileName
已經打開。如果是開放elsehwere那麼只讀版本打開到最新頁面
Sub TestFileOpened()
Dim Wb As Workbook
Dim StrFileName As String
Dim objFSO As Object
Dim objTF As Object
Dim strLogTxt As String
Dim arrStr
StrFileName = "c:\temp\main.xlsm"
If Dir(StrFileName) = vbNullString Then
MsgBox StrFileName & " does not exist", vbCritical
Exit Sub
End If
If IsFileOpen(StrFileName) Then
Set Wb = Workbooks.Open(StrFileName, , True)
If Dir(Wb.Path & "\log.txt") <> vbNullString Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTF = objFSO.OpenTextFile(Wb.Path & "\log.txt", 1)
Do Until objTF.AtEndOfStream
strLogTxt = objTF.ReadLine
Loop
objTF.Close
arrStr = Split(strLogTxt, ";")
On Error Resume Next
If Not IsEmpty(arrStr) Then
Wb.Sheets(arrStr(0)).Activate
If Err.Number <> 0 Then MsgBox arrStr(0) & " could not be activate"
End If
On Error GoTo 0
End If
Else
Set Wb = Workbooks.Open(StrFileName)
End If
End Sub
' This function checks to see if a file is open or not. If the file is
' already open, it returns True. If the file is not open, it returns
' False. Otherwise, a run-time error occurs because there is
' some other problem accessing the file.
Function IsFileOpen(filename As String)
Dim filenum As Integer, errnum As Integer
On Error Resume Next ' Turn error checking off.
filenum = FreeFile() ' Get a free file number.
' Attempt to open the file and lock it.
Open filename For Input Lock Read As #filenum
Close filenum ' Close the file.
errnum = Err ' Save the error number that occurred.
On Error GoTo 0 ' Turn error checking back on.
' Check to see which error occurred.
Select Case errnum
' No error occurred.
' File is NOT already open by another user.
Case 0
IsFileOpen = False
' Error number for "Permission Denied."
' File is already opened by another user.
Case 70
IsFileOpen = True
' Another error occurred.
Case Else
Error errnum
End Select
End Function
我擱置下去,因爲它是一個大量的工作來實現只是爲了獲得一個精密這一解決方案。無論我是否決定使用它,我都很感激幫助。 – Wes 2012-04-20 00:21:29
+ 1很好完成 – 2012-04-20 18:18:36