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/30000104Microsoft Advisory Services -
http://support.microsoft.com/gp/advisoryserviceFor 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:
- In Microsoft Excel, open a new workbook.
- On the File menu, click Save As.
- In the Save in box, browse to find C.
- In the File name box, type Test, and then click Save.
- Close Test.xls.
- Start Windows Explorer.
- Browse to find C:\test.xls.
- Right-click test.xls.
- On the shortcut menu, click Properties.
- 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:
- 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
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:
- Start Excel, and then press ALT+F11 to start the Visual Basic editor.
- On the Insert menu, click Module.
- 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
- Press ALT+F11 to return to Excel.
- On the Tools menu, point to Macro, and then click Macros.
- Click Example2, and then click Run.
When you run the macro, you receive the following message:
File was not opened as read-only