Microsoft KB Archive/213266

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

Article ID: 213266

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 2000 Standard Edition

-



This article was previously published under Q213266





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.



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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

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.

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:  Start Excel, and then press ALT+F11 to start the Visual Basic editor. On the Insert menu, click Module.  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  Press ALT+F11 to return to Excel. On the Tools menu, point to Macro, and then click Macros. 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

</li></ol>

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: <ol> Start Excel, and then press ALT+F11 to start the Visual Basic editor.</li> On the Insert menu, click Module.</li>  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 </li> Press ALT+F11 to return to Excel.</li> On the Tools menu, point to Macro, and then click Macros.</li> Click Example2, and then click Run. When you run the macro, you receive the following message:

File was not opened as read-only

</li></ol>

<div class="references_section">