0

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.

Community
  • 1
  • 1
  • 1
    In your comment you write `ff = FreeFile_()` but in your code `ff = FreeFile()`. Initial code without underscore works fine. – v.grabovets Jan 11 '17 at 10:38
  • Yes i know... In my code i wrote ff = FreeFile() but when I get error it has underscore in it. – DejanBlažič Jan 11 '17 at 10:43
  • Try `ff = FreeFile` without parentheses. I have Excel 2016 and it works with or without them fine. – v.grabovets Jan 11 '17 at 10:52
  • yeps, that works. but I got error in next line... the underscore showed right before Lock word. so it is like Syntax error on line 16: Open FileName For Input_Lock Read As #ff – DejanBlažič Jan 11 '17 at 10:58

1 Answers1

0

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.

D. O.
  • 616
  • 1
  • 11
  • 25
  • Hello, thanks for answer. I rewrote whole code, but there was same error... i did get past it, but than i get stuck at the last line in case... again syntax error.. – DejanBlažič Jan 12 '17 at 10:58
  • do you mean error at line "Case Else: Error ErrNo"? If so, then replace "ErrNo = Err" by "ErrNo = Err.Number" – D. O. Jan 12 '17 at 15:20
  • Yes, that's the error.. it says syntax error Case Else: _Error ErrNo Tried the replace stuff, but i get same error.. Syntax error on that line. I am really confused now :/ – DejanBlažič Jan 13 '17 at 05:32
  • In your code, delete the blank between "Else: Error", then rewrite it in the same place. – D. O. Jan 13 '17 at 15:15