opening a workbook as read only

V

Valeria

Dear experts,
I have a workbook containing macros which I would like to open as read-only
on certain days.
E.g.:
If Day(Now) > 9 And Day(Now) < 17 Then
'OPEN WORKBOOK AS READ ONLY
msgbox ("You can only access this spreadsheet between the 17th of the month
and the 8th of the following month")
Else
'OPEN WORKBOOK NORMALLY
End If

Is this possible? what would be the code for it? And would my macros still
work?

Thanks!
Kind regards
 
J

john

this should do what you want. Place in standard module.

Sub OpenBook()
Dim myfile As String
Dim passwrd As String
Dim wb As Workbook

'change file path & name as required
myfile = "C:\myfolder\myfilename.xls"

'change password as required
passwrd = "mypassword"

If Day(Now) > 9 And Day(Now) < 17 Then
'OPEN WORKBOOK AS READ ONLY
msg = MsgBox("You can only access this spreadsheet between" &
Chr(10) & _
"the 17th of the month and the 8th of the following
month", vbInformation, "Open Workbook")

Set wb = Workbooks.Open(myfile, ReadOnly:=True, Password:=passwrd)

Else
'OPEN WORKBOOK NORMALLY

Set wb = Workbooks.Open(myfile, ReadOnly:=False, Password:=passwrd)

End If

End Sub
 
V

Valeria

Hi John,
thanks, however I am referring to the same workbook where the code is placed
who should open as read only or normally... it is not a separate one.

I guess I should put some code in the Workbook_Open procedure but I am not
sure how to tell my workbook to open itself as read only! (if it's possible
at all).
Thanks,
Kind regards
 
J

john

Don't think that is possible once file has been opened but could be wrong!

My understanding is that by default, all files created and opened in
Microsoft Excel are read write files. This applies not only to Microsoft
Excel but almost all other applications. Also, a file cannot be created read
only just because if you create a read only file you can never edit it. But
you can make a file read only so that others don't tamper with it.

To make a Excel file read only (Microsoft Excel 2003),


Goto Tools ->Options->Security (The Tools menu in the worksheet menu bar)

Check the READ-ONLY RECOMMENDED box.
This would make your file read only and when the file is opened, it would
prompt you with the message box whether you want the file to be opened in
read only mode or not. If you click YES, the file would open in read only
mode. If you click NO, the file would open in read write mode and you can
save your changes.

For Microsoft Excel 2000 and other previous versions

Goto File->Save As
Click the TOOLS button on the right corner and select GENERAL OPTIONS
Now check the READ-ONLY RECOMMENDED box.
This works well in Microsoft Excel 2003 also.

The above option, though making the file read only, gives the user the
option to edit it. So even if an user unknowingly presses the NO button ,
when prompted for read only opening, he may still be able to edit the file.
If you want to prevent this then,

Navigate to your file through Windows Explorer or the File Open Box and
select it.

RIGHT CLICK and select PROPERTIES
Check the READ ONLY box.
This always open the file read only and even if you already made your file
read only, by using the first option, this overrides it since this function
is part of the operating system rather than the application. To make the file
read write again, just uncheck the READ ONLY box.

This may not be what you were looking for but may give you another idea how
to resolve your requirement

Hope helpful
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top