Check if excel workbook is open?

2019-09-05 21:53发布

So... I used this piece of code for reference:

Option Explicit

Sub Sample()
Dim Ret

Ret = IsWorkBookOpen("C:\myWork.xlsx")

If Ret = True Then
    MsgBox "File is open"
Else
    MsgBox "File is Closed"
End If
End Sub

Function IsWorkBookOpen(FileName As String)
Dim ff As Long, ErrNo As Long

On Error Resume Next
ff = FreeFile()
Open FileName For Input Lock Read As #ff
Close ff
ErrNo = Err
On Error GoTo 0

Select Case ErrNo
Case 0:    IsWorkBookOpen = False
Case 70:   IsWorkBookOpen = True
Case Else: Error ErrNo
End Select
End Function

I used my workbook and everything, but it returns Syntax error on line 15: ff = FreeFile_().

Why is this the case? What syntax error is there? I am trying to check if workbook is opened by some other user, because if that's the case, I can't save the values in the Workbook.

Thanks for answers, D.

1条回答
smile是对你的礼貌
2楼-- · 2019-09-05 22:31

I think you done copy/paste the code into VBA editor. So clean the code, delete all blancks that are not needed, verify where each line is finished.

查看更多
登录 后发表回答