Results 1 to 9 of 9

Thread: Excel - save read only and protected VBA

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    111

    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:
    1. ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    2.     ActiveSheet.EnableSelection = xlNoSelection
    3.     Range("B7").Select
    4.     ActiveWorkbook.SaveAs Filename:= _
    5.         "C:\Documents and Settings\Administrator\My Documents\Book2.xls", FileFormat _
    6.         :=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:= _
    7.         False, CreateBackup:=False
    8.     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

  2. #2
    Frenzied Member
    Join Date
    Aug 2005
    Posts
    1,042

    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    111

    Re: Excel - save read only and protected VBA

    Thanks

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

  4. #4
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Excel - save read only and protected VBA

    Have you tried:

    VB Code:
    1. ActiveWorkbook.Protect


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

    zaza
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    111

    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!

  6. #6
    New Member
    Join Date
    Feb 2006
    Posts
    3

    Re: Excel - save read only and protected VBA

    Try this in the workbook open event

    VB Code:
    1. Private Sub Workbook_Open()
    2. ProtectIt
    3. End Sub
    4.  
    5. Sub ProtectIt()
    6. For SCount = 1 To Sheets.Count
    7. Worksheets(SCount).Protect Password:="KeepOut", DrawingObjects:=True, Contents:=True, Scenarios:=True
    8. Worksheets(SCount).EnableSelection = xlNoSelection 'xlUnlockedCells
    9. Next SCount
    10. End Sub

    Password is optional,

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    111

    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?

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    111

    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?

  9. #9
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excel - save read only and protected VBA

    Quote 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
  •  



Click Here to Expand Forum to Full Width