Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

This article was previously published under Q213266

↑ Back to the top


Summary

This article describes how to programmatically determine whether a Microsoft Excel file has an attribute of read-only, and how to determine whether a file is opened as read-only.

↑ Back to the top


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 professionals 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 a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS To determine whether a file has an attribute of read-only or whether the file is opened as read-only, use the sample macros described in this article.

Set a File to Read-Only

To set a file to read-only, follow these steps:
  1. In Microsoft Excel, open a new workbook.
  2. On the File menu, click Save As.
  3. In the Save in box, browse to find C.
  4. In the File name box, type Test, and then click Save.
  5. Close Test.xls.
  6. Start Windows Explorer.
  7. Browse to find C:\test.xls.
  8. Right-click test.xls.
  9. On the shortcut menu, click Properties.
  10. On the General tab, in the Attributes pane, click to select the Read-only check box, and then click OK.

Sample Visual Basic Procedures

Example 1: Macro to Determine If File Is Read-Only

To create and run a macro that determines whether a file named C:\test.xls is read-only, use the steps in the following example:
  1. Start Excel, and then press ALT+F11 to start the Visual Basic editor.
  2. On the Insert menu, click Module.
  3. In the module sheet, type the following code:
    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
    						
  4. Press ALT+F11 to return to Excel.
  5. On the Tools menu, point to Macro, and then click Macros.
  6. Click Example1, and then click Run. If you followed the steps in the "Set a File to Read-Only" section, you receive the following message when you run the macro:
    File is Read-only

Example 2: Macro to Determine If Active Workbook Is Opened as Read-Only

To create and run a macro that determines whether the active workbook is read-only, use the steps in the following example:
  1. Start Excel, and then press ALT+F11 to start the Visual Basic editor.
  2. On the Insert menu, click Module.
  3. In the module sheet, enter the following code:
    Sub Example2()
        ' Check to see if the active workbook was
        ' opened as read-only in 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
    						
  4. Press ALT+F11 to return to Excel.
  5. On the Tools menu, point to Macro, and then click Macros.
  6. Click Example2, and then click Run. When you run the macro, you receive the following message:
    File was not opened as read-only

↑ Back to the top


References

For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:
212536� OFF2000: How to Run Sample Code from Knowledge Base Articles

↑ Back to the top


Keywords: KB213266, kbprogramming, kbhowto, kbdtacode

↑ Back to the top

Article Info
Article ID : 213266
Revision : 8
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 274