Last active
June 14, 2019 07:05
-
-
Save DuongAQ/303a5e9438ade3bee9254f94c0c624c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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