Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Excel > Script to unprotect a worksheet, edit, re-protect?


Reply
 
Thread Tools Display Modes
  #1  
Old 09-28-2004, 06:46 AM
GFritz34 GFritz34 is offline
Centurion
 
Join Date: Aug 2004
Posts: 173
Default Script to unprotect a worksheet, edit, re-protect?


I'm using Excel to store some historical user events. I don't want a user to be able to open these sheets offline w/out a password, therefore I need protection on.

How can I do this in VBA?
Reply With Quote
  #2  
Old 09-28-2004, 07:02 AM
Greychild's Avatar
Greychild Greychild is offline
Centurion
 
Join Date: Jan 2004
Location: Hong Kong
Posts: 176
Default

Code:
Activesheet.Unprotect("MyPassword") 'your code Activesheet.Protect("MyPassword")
Reply With Quote
  #3  
Old 09-28-2004, 08:25 AM
Wheels1978 Wheels1978 is offline
Junior Contributor
 
Join Date: Apr 2004
Location: 's-Gravenpolder, NL
Posts: 389
Default

I thought it was
Code:
ActiveSheet.Unprotect password:="MyPassword" 'your code ActiveSheet.Protect password:="MyPassword"
Maybe they both work. Haven't tried Greychild's code yet.
__________________
"Accept what you can not change and change what you can not accept" - Me
"Normal is the average of all oddities" - Me
"Don't re-invent the wheel!" - My dad
Please enclose your code in [vb] and [/vb] tags
Reply With Quote
  #4  
Old 09-28-2004, 09:35 AM
GFritz34 GFritz34 is offline
Centurion
 
Join Date: Aug 2004
Posts: 173
Default

Quote:
Originally Posted by Greychild
Code:
Activesheet.Unprotect("MyPassword") 'your code Activesheet.Protect("MyPassword")

This is working, thanks alot. I was trying something similar but on the application not the sheet so that was why it wasn't working.
Reply With Quote
  #5  
Old 09-28-2004, 11:15 AM
Greychild's Avatar
Greychild Greychild is offline
Centurion
 
Join Date: Jan 2004
Location: Hong Kong
Posts: 176
Default

Happy to help - don't forget the macro recorder is often a great place to start with things like that.

Wheels, your code is equivalent. Your code uses a named argument, whereas mine takes advantage of the fact that the first argument for Protect is the password. If it were the 10th argument, I would either have to use your method, or use a lot of commas with nothing in between:

Code:
'This is an example, not real code Activesheet.Protect(,,,,,,,,,"MyPassword")

The commas with nothing in between mean that I am accepting the defaults for the 9 preceding optional arguments. Obviously, this would make the code very hard to read (and write).
Reply With Quote
  #6  
Old 09-29-2004, 01:18 AM
Wheels1978 Wheels1978 is offline
Junior Contributor
 
Join Date: Apr 2004
Location: 's-Gravenpolder, NL
Posts: 389
Default

Quote:
Originally Posted by Greychild
Wheels, your code is equivalent.
I thought it was like that. The help also mentions the stuff between commas. Personally I prefer the named arguments, just for readability. I have had some bad experience with an ex-colleague of mine, who didn't even put comments in his code. That's why I never even think about the brackets and commas...
__________________
"Accept what you can not change and change what you can not accept" - Me
"Normal is the average of all oddities" - Me
"Don't re-invent the wheel!" - My dad
Please enclose your code in [vb] and [/vb] tags
Reply With Quote
  #7  
Old 09-29-2004, 08:46 AM
italkid's Avatar
italkid italkid is offline
Down...

Retired Moderator
* Expert *
 
Join Date: Dec 2002
Location: Belgium.
Posts: 6,731
Default

You could look into the "UserInterFaceOnly" argument too...
Reply With Quote
  #8  
Old 09-29-2004, 11:27 PM
Ivan F Moala's Avatar
Ivan F Moala Ivan F Moala is offline
Junior Contributor

* Expert *
 
Join Date: Dec 2002
Location: Auckland, New Zealand
Posts: 201
Default

Quote:
Originally Posted by italkid
You could look into the "UserInterFaceOnly" argument too...
Yep, that would be the prefered way to go as long as you set it upon Open.
__________________
Ivan F Moala
Visit my Site
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->