Check if a file is write protecteb before opening it

Silverryggen

New Member
Joined
Jan 25, 2010
Messages
5
Hi,

I am trying to create a function in VBA that checks if the target file is write protected before opening it. I have tried the code below without any luck :confused:

Filename is a variable passed in from the main sub.

Hopefully someone knows the answer to this problem :biggrin:

Function Protected (Filename) As Boolean
Dim test As Boolean

On Error Resume Next
Application.DisplayAlerts = False

If Workbooks(Filename).ReadOnly Then
Protected = True
Else
Protected = False
End If

Application.DisplayAlerts = True
End Function
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi

Try this:

Code:
Function IsFileWriteable(strFile As String) As Boolean
Dim i As Integer
i = FreeFile
On Error Resume Next
Open strFile For Binary Access Write As #i
Close #i
IsFileWriteable = (Err = 0)
End Function
 
Upvote 0
Richard,
Does strFile have to be an OPEN file?
Is there a tutorial on what each line of your code means/does?
Jim May
 
Upvote 0
Jim

strFile is just the full name of a file (presumably closed, as the OP wanted to check before it was opened) eg like "C:\Somefolder\Book1.xls".

The code otherwise just uses standard VBA functions. The important bit is that it attempts to open the file (using the Open statement) for write access. If it can't then the file is read-only, if it can then it is writeable.
 
Upvote 0
Testing this a bit.. leads to the following q..

If the Function returns TRUE - then the Workbook is Unprotected?

I created a New WB - and Simply did a Tools, Protection, Workbook. checked both Structure and Windows; applied Password and Closed Filename c:\Myfolder\Test.xls

In my WB where I have your Function Code
I added the code line Application.Volatile
and in cell K1 I entered =Today()
I enter into a cell (A1):
=IsFileWriteable("c:\Myfolder\Test.xls")

Currently the formula is TRUE << seems like it should be FALSE (since the WB is protected).. Confused here,, can you assist?

Jim
 
Upvote 0
The function checks if a file is write-protected (as per OP's original request) - it doesn't check if a file has workbook/sheet protection.
 
Upvote 0
Since He asked "if the target file is write protected" -- I guess that means
If the file is READ-ONLY... OK, I now see it.
But it brings up an interesting Q
Can a function (udf) dertermine if an Unopened file is WB Protected?
Thanks,
Jim
 
Upvote 0

Forum statistics

Threads
1,215,479
Messages
6,125,041
Members
449,206
Latest member
Healthydogs

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