Checking for Read-Only on open

cyberwolf

New Member
Joined
Oct 5, 2007
Messages
24
Office Version
  1. 365
Platform
  1. Windows
How would I check a file if it is read-only when I am opening it? I have a module in the ThisWorkbook area and I want to add something that checks the file for Read-Only and then does not execute the code.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi

The following UDF will tell you whether a file can be currently opened with Write-access:

Code:
Function IsFileWriteable(StrFilePath As String) As Boolean
    Dim FileNum As Integer
    IsFileWriteable = False
    FileNum = FreeFile
    On Error Resume Next
    Open StrFilePath For Input Lock Read Write As #FileNum  ' Open file and lock it.
    If Err.Number = 0 Then IsFileWriteable = True 'Can write to file
    Close FileNum
End Function

Use in code like:

Code:
If IsFileWriteable("C:\myFile.xls") Then
   Workbooks.Open "C:\myFile.xls"
   '...
End If
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,800
Members
449,468
Latest member
AGreen17

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