|
-
Mar 26th, 2006, 02:06 PM
#1
Thread Starter
Lively Member
Excel - save read only and protected VBA
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:
VB 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
-
Mar 26th, 2006, 02:59 PM
#2
Frenzied Member
Re: Excel - save read only and protected VBA
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
-
Mar 26th, 2006, 04:39 PM
#3
Thread Starter
Lively Member
Re: Excel - save read only and protected VBA
Thanks
That does work but I need to do it using vba during execution.
-
Mar 26th, 2006, 04:59 PM
#4
Re: Excel - save read only and protected VBA
Have you tried:
Look it up in VBA Help for more info. You can set the password to help you Unprotect it.
zaza
-
Mar 26th, 2006, 06:04 PM
#5
Thread Starter
Lively Member
Re: Excel - save read only and protected VBA
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!
-
Mar 27th, 2006, 08:00 AM
#6
New Member
Re: Excel - save read only and protected VBA
Try this in the workbook open event
VB Code:
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,
-
Mar 27th, 2006, 09:53 AM
#7
Thread Starter
Lively Member
Re: Excel - save read only and protected VBA
Thanks - BUT, the workbook is automatically created as an output file from Access - can't add code dynamically!!! OR can you?
-
Mar 28th, 2006, 10:40 AM
#8
Thread Starter
Lively Member
Re: Excel - save read only and protected VBA
Any more ideas? This one's still not resolved ...
By the way - is it possible to add code dynamically?
-
Mar 28th, 2006, 10:50 AM
#9
Re: Excel - save read only and protected VBA
 Originally Posted by Pobo
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.
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|