PDA

Click to See Complete Forum and Search --> : Excel - save read only and protected VBA


Pobo
Mar 26th, 2006, 01:06 PM
Hi

Can anyone tell me the best way to automate the saving of a file as read only and to protect the worksheet so that cells cannot be edited or copied.

At the moment I am using the following code:

ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveSheet.EnableSelection = xlNoSelection
Range("B7").Select
ActiveWorkbook.SaveAs Filename:= _
"C:\Documents and Settings\Administrator\My Documents\Book2.xls", FileFormat _
:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:= _
False, CreateBackup:=False
ActiveWorkbook.Close

But when you reopen the workbook the cells cannot be edited but the dis-'EnableSelection' setting seems to have been reset and users are able to select cells again.!!!!??

Has anyone got any suggestions please?


Many thanks

Pobo

AIS4U
Mar 26th, 2006, 01:59 PM
Pobo:

I am not really an Excel guy, but have you tried setting the protection through Tools>Protection>Protect Sheet in your Excel document.

I think if you did that then the protection would exist even when you do your Visual Basic manipulation or the Excel wookbook.

I may be wrong.

Good Luck

Pobo
Mar 26th, 2006, 03:39 PM
Thanks

That does work but I need to do it using vba during execution.

zaza
Mar 26th, 2006, 03:59 PM
Have you tried:

ActiveWorkbook.Protect


Look it up in VBA Help for more info. You can set the password to help you Unprotect it.

zaza

Pobo
Mar 26th, 2006, 05:04 PM
Yes, I have but when you close and reopen the workbook you can select the cells. If you do it manually using the Tools menu options when you reopen the workbook you can't select the cells but if you do the same thing with vba you can!

lexII
Mar 27th, 2006, 07:00 AM
Try this in the workbook open event

Private Sub Workbook_Open()
ProtectIt
End Sub

Sub ProtectIt()
For SCount = 1 To Sheets.Count
Worksheets(SCount).Protect Password:="KeepOut", DrawingObjects:=True, Contents:=True, Scenarios:=True
Worksheets(SCount).EnableSelection = xlNoSelection 'xlUnlockedCells
Next SCount
End Sub


Password is optional,

Pobo
Mar 27th, 2006, 08:53 AM
Thanks - BUT, the workbook is automatically created as an output file from Access - can't add code dynamically!!! OR can you?

Pobo
Mar 28th, 2006, 09:40 AM
Any more ideas? This one's still not resolved ...

By the way - is it possible to add code dynamically?

DKenny
Mar 28th, 2006, 09:50 AM
Yes, I have but when you close and reopen the workbook you can select the cells. If you do it manually using the Tools menu options when you reopen the workbook you can't select the cells but if you do the same thing with vba you can!

Just because you have the worksheet protected, it doesn't mean that the individual cells are protected. Goto the Fomat->Cells menu and select the Protection tab. If the cell is not "Locked" then the worksheet protection will have no effect.