Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Detect Password Protection on a Sheet

Hi,

does anyone know how to detect whether a sheet is Password
Protected as opposed to just Protected.

I do this by writing a function and returning true when I
attempt to unprotect it using an "on error". There must be
a better way...

Chris
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Detect Password Protection on a Sheet

This code here does what I do believe you are looking for it to do. It
is a little ugly using errors to test for the password but it works.

Sub CheckProtection()

If Sheet1.ProtectContents = True Then
MsgBox "I am protected!"
On Error GoTo MustBePassword
Sheet1.Unprotect "ZFNUefnisudr7tqw6etr90er" ''Password that
no one would ever use.
Sheet1.Protect ''Used to protect sheet back up if no
password protecting it.
On Error GoTo 0
Else
MsgBox "I am not protected!"
End If

AllDone:
Exit Sub

MustBePassword:
MsgBox "I am also Password Protected!!"
GoTo AllDone

End Sub


Hope that helps,
Keith
www.kjtfs.com


---
Message posted from http://www.ExcelForum.com/

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 493
Default Detect Password Protection on a Sheet

Still relying on errors handling, but perhaps a little cleaner:

Public Function CheckProtection(ByRef wkSht As Worksheet) As Long
CheckProtection = wkSht.ProtectContents
If CheckProtection Then
On Error Resume Next
wkSht.Unprotect ""
On Error GoTo 0
If wkSht.ProtectContents Then
CheckProtection = -CheckProtection
Else
wkSht.Protect
End If
End If
End Function


This will return the following:

0/False if not protected
-1/True if protected without a password
1 if protected with a password.




If ActiveSheet.ProtectCIn article ,
KJTFS wrote:

This code here does what I do believe you are looking for it to do. It
is a little ugly using errors to test for the password but it works.

Sub CheckProtection()

If Sheet1.ProtectContents = True Then
MsgBox "I am protected!"
On Error GoTo MustBePassword
Sheet1.Unprotect "ZFNUefnisudr7tqw6etr90er" ''Password that
no one would ever use.
Sheet1.Protect ''Used to protect sheet back up if no
password protecting it.
On Error GoTo 0
Else
MsgBox "I am not protected!"
End If

AllDone:
Exit Sub

MustBePassword:
MsgBox "I am also Password Protected!!"
GoTo AllDone

End Sub


Hope that helps,
Keith
www.kjtfs.com


---
Message posted from http://www.ExcelForum.com/

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Password protection in macro ( Anybody can view my password in VB Sherees Excel Discussion (Misc queries) 2 January 24th 10 10:05 PM
Excel Data Protection Best Practice: AKA: Real Sheet Protection Mushman(Woof!)[_2_] Excel Discussion (Misc queries) 4 December 30th 09 01:20 AM
Excel Data Protection- AKA: Sheet/Macro Password Protection Mushman(Woof!) Setting up and Configuration of Excel 0 December 29th 09 06:50 AM
Password protection of Excel Sheet linked to Bloomberg Data Rohit Excel Discussion (Misc queries) 0 June 28th 05 10:29 AM
How to detect if sheet is hidden? hstijnen Excel Worksheet Functions 1 March 24th 05 02:40 PM


All times are GMT +1. The time now is 09:18 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"