How to Determine If a File Is Read-Only
Steps to Set a File to Read-Only Using Windows
- In Microsoft Excel, open up a new workbook and save it to the C:\ folder (directory) with the name C:\Test.xls.
- Close Test.xls, and in Windows Explorer (Windows File Manager in versions earlier than Windows 95), select the file you saved in Step 1.
- On the File menu, click Properties.
- In the file's Properties box, click to select the Read-only check box, and then click OK.
Steps to Set a File to Read-Only using the Macintosh
- In Microsoft Excel, open a new workbook and save it as "Test" to the hard disk.
- Close Test and hide Microsoft Excel. On the desktop, open your hard disk by double-clicking the hard disk icon.
- Highlight "Test," click the File menu, and then click Get Info.
- Click on Locked, then click on the close button in the upper-left corner of the Info box.
Sample Visual Basic ProceduresExample 1: A Macro to Determine if File's Read-Only Attribute is Set
The following macro will determine if a file's read-only attribute is set.Open the file you created above, and then type the macros below in a modulesheet.
Sub Example1() ' Test to see if the Read-only attribute was assigned to the file. If GetAttr("c:\test.xls") And vbReadOnly Then MsgBox "File is Read-only" Else MsgBox "File is not read-only" End IfEnd Sub
Note: If you are working on a Macintosh, substitute the harddisk's name and"test" for "c:\test.xls". For example, if your harddisk's name is "HD",you would type GetAttr("HD:test").
Example 2: A Macro to Determine if Active Workbook is Opened as Read-Only
The following macro will determine if the active workbook is opened asread-only.
Sub Example2() ' Check to see if the active workbook was ' opened as read-only within Microsoft Excel. If ActiveWorkbook.ReadOnly Then MsgBox "File was opened as read-only" Else MsgBox "File was not opened as read-only" End IfEnd Sub
Article ID: 153058 - Last Review: 12/04/2015 14:59:10 - Revision: 2.3
- kbnosurvey kbarchive kbdtacode kbhowto kbprogramming KB153058