简体   繁体   English

检查excel工作簿是否打开?

[英]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_(). 我使用了工作簿和所有内容,但在第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. 感谢您的回答,D。

I think you done copy/paste the code into VBA editor. 我认为您已将代码复制/粘贴到VBA编辑器中。 So clean the code, delete all blancks that are not needed, verify where each line is finished. 因此,清理代码,删除不需要的所有空白,验证每行的结束位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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