简体   繁体   中英

Check if excel workbook is open?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM