Как предотвратить открытие одного и того же листа - PullRequest
0 голосов
/ 16 июня 2020

У меня есть несколько листов, которыми вы поделились через Dropbox. Мне нужно определить, открыт ли уже файл на другом устройстве.

Я пробовал приведенный ниже код, но проблема в том, что каждый раз, когда я открываю книгу, он сообщает, что файл открыт.

Function IsFileOpen(fileName As String)

Dim fileNum As Integer
Dim errNum As Integer

'Allow all errors to happen
On Error Resume Next
fileNum = FreeFile()

'Try to open and close the file for input.
'Errors mean the file is already open
Open fileName For Input Lock Read As #fileNum
Close fileNum

'Get the error number
errNum = Err

'Do not allow errors to happen
On Error GoTo 0

'Check the Error Number
Select Case errNum

    'errNum = 0 means no errors, therefore file closed
    Case 0
    IsFileOpen = False

    'errNum = 70 means the file is already open
    Case 70
    IsFileOpen = True

    'Something else went wrong
    Case Else
    IsFileOpen = errNum

End Select

End Function
Private Sub Workbook_Open()
Dim fileName As String
fileName = ActiveWorkbook.FullName

'Call function to check if the file is open
If IsFileOpen(fileName) = False Then

    MsgBox "YOU ARE GOOD TO GO"

Else

    'The file is open or another error occurred
    MsgBox " STOP THIS FILE IS ALREADY OPEN."

End If

End Sub
...