Skip to content

Instantly share code, notes, and snippets.

@DuongAQ
Last active June 14, 2019 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DuongAQ/303a5e9438ade3bee9254f94c0c624c5 to your computer and use it in GitHub Desktop.
Save DuongAQ/303a5e9438ade3bee9254f94c0c624c5 to your computer and use it in GitHub Desktop.
Function IsFileOpen(fileName As String) 'Function kiểm tra file đang mở hay đóng
Dim fileNum As Integer
Dim errNum As Integer
'Bỏ qua các lỗi có thể gặp
On Error Resume Next
fileNum = FreeFile()
'Thực hiện việc mở / đóng file xem có lỗi không
'Lỗi xảy ra có nghĩa là file đang mở
Open fileName For Input Lock Read As #fileNum
Close fileNum
'Lấy mã lỗi
errNum = Err
'Đặt cơ chế bẫy lỗi về mặc định
On Error GoTo 0
'Kiểm tra theo mã lỗi
Select Case errNum
'Mã lỗi = 0 có nghĩa file đang đóng
Case 0
IsFileOpen = False
'Mã lỗi = 70 có nghĩa file đang mở
Case 70
IsFileOpen = True
'Các lỗi khác
Case Else
IsFileOpen = errNum
End Select
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment