XL: How to Determine If a File Is Read-Only

Last reviewed: February 3, 1998
Article ID: Q153058
The information in this article applies to:
  • Microsoft Excel 97 for Windows
  • Microsoft Excel for Windows 95, versions 7.0, 7.0a
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for the Macintosh, versions 5.0, 5.0a
  • Microsoft Excel 98 Macintosh Edition

SUMMARY

This article describes how to determine if a file has an attribute of read- only, and how to determine if a file is opened as read-only using Microsoft Visual Basic for Applications code.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

You can determine if a file has an attribute of read-only or if the file is opened as read-only by using the macros described in this article.

Steps to Set a File to Read-Only Using Windows

  1. In Microsoft Excel, open up a new workbook and save it to the C:\ folder (directory) with the name "C:\Test.xls."

  2. Close Test.xls, and in Windows Explorer (Windows File Manager in versions earlier than Windows 95), select the file you saved in Step 1.

  3. On the File menu, click Properties.

  4. 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

  1. In Microsoft Excel, open a new workbook and save it as "Test" to the hard disk.

  2. Close Test and hide Microsoft Excel. On the desktop, open your hard disk by double-clicking the hard disk icon.

  3. Highlight "Test," click the File menu, and then click Get Info.

  4. Click on Locked, then click on the close button in the upper-left corner of the Info box.

Sample Visual Basic Procedures

Example 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 module sheet.

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 If

End Sub

If you followed steps 1 - 5 in the "Steps to Set a File to Read-Only using Windows" section, when you run the macro in Example 1, you will receive a message box with the "File is Read-only" message.

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 as read-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 If

End Sub

If you open the workbook created in step 1 of the "Steps to Set a File to Read-Only using Windows" section, when you run the macro from Example 2, you will receive a message box with the "File was opened as read-only" message.

REFERENCES

For additional information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q163435
   TITLE     : VBA: Programming Resources for Visual Basic for
               Applications


Additional query words: 5.00 5.00a 5.00c 7.00 7.00a 8.00 readonly howto
Keywords : kbcode kbprg PgmHowto
Version : WINDOWS: 5.0,5.0c,7.0,7.0a,97; MACINTOSH: 5.0,5.0a,98
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 3, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.