Check if File locked in excel 2016

Igor88

New Member
Joined
Apr 27, 2017
Messages
5
Hi ,

I found this function , but I get an error :75

What I am doing wrong?



Option Explicit


Sub Sample()
Dim Ret

Ret = IsWorkBookOpen("C:\myWork.<wbr style="font-family: arial, sans-serif; font-size: 12.8px;">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
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Err.Number = 75 if readonly
Err.Number = 70 if open
and return false of not open
 
Upvote 0
Try modified code below

Code:
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, ErrNoDescription As String
    On Error Resume Next
    ff = FreeFile()
    Open FileName For Input Lock Read As #ff
    Close ff
    ErrNoDescription = Err.Description
    ErrNo = Err
    On Error GoTo 0
    Select Case ErrNo
    Case 0:    IsWorkBookOpen = False
    Case 70:   IsWorkBookOpen = True
    Case Else: MsgBox ErrNo & " " & ErrNoDescription, vbCritical
    End Select
End Function
 
Upvote 0
Hi,

I always get error = 75 - I thinck it because microsoft changed in excel 2016 the default of openning files to read only
 
Upvote 0

Forum statistics

Threads
1,213,513
Messages
6,114,072
Members
448,546
Latest member
KH Consulting

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top