Microsoft KB Archive/113786

= Protect Method Fails When Module Is Hidden =

Article ID: 113786

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Excel 95 Standard Edition
 * Microsoft Excel 5.0c
 * Microsoft Excel 5.0a for Macintosh

-



This article was previously published under Q113786



SYMPTOMS
In Microsoft Excel, the Visual Basic Protect method (syntax 2) or Unprotect method may fail when you attempt to protect or unprotect a workbook if the following conditions are true:
 * The module that contains the Protect or Unprotect command is hidden -and-


 * The Protect or Unprotect command is being applied to the workbook that it is contained in (it is trying to protect or unprotect its own workbook)



WORKAROUND
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To change the protection status of a workbook using a Visual Basic subroutine contained in that workbook, use any of the following methods.

Method 1
Use the SendKeys statement to protect or unprotect the workbook. Note that this method works with Microsoft Excel for Windows (you cannot send keystrokes to applications running under the Macintosh operating system).

The following example uses the SendKeys statement to send the key strokes necessary to protect the workbook. The OnTime command is used to resume another macro one second later. Note that Application.ScreenUpdating will not affect this procedure because the macro is exited and then the key strokes are executed from the keyboard buffer.

You should enter the following code as the last piece of code in the module. ' You can skip this line if you are adding this code to the ' end of an existing macro Sub ProtectWorkbook

' This will return to a macro named My_Macro after protecting ' the workbook. This line is not necessary if you do not need ' to resume after the workbook is protected Application.OnTime Now + TimeValue("00:00:01"), "My_Macro"

' Note that you can use %(tpp) instead of %(tpw) to  ' protect only the active sheet. Application.SendKeys "%(tpw){ENTER}"

' The procedure must end before the keys will be sent End Sub

Method 2
Unhide the module that contains the subroutine before using the Protect method. Note that if you use this method, you cannot protect the structure of the workbook, and you cannot use the Unprotect method (because if the structure of the workbook is currently protected, you cannot hide or unhide a sheet in the workbook).

To use a subroutine contained in a hidden module ("ModuleName") to protect the workbook (not the structure) in which it is contained, you could use the following code: ' Unhide the module that contains this subroutine. ThisWorkbook.Modules("ModuleName").Visible = True

' Protect the workbook by using the Protect method. ThisWorkbook.Protect Structure:=False, Windows:=True

' Re-hide the module that contains this subroutine. ThisWorkbook.Modules("ModuleName").Visible = False

Method 3
Set the Visible property of the Module containing the Protect and Unprotect macros to xlVeryHidden rather than False. The Structure and Windows arguments are optional.

For example: ThisWorkbook.Modules("ModuleName").Visible = xlVeryHidden



STATUS
Microsoft has confirmed this to be a problem in the versions of Microsoft Excel listed at the beginning of this article.



MORE INFORMATION
When you use the Protect method to protect a workbook that contains the running subroutine, if the module that contains that subroutine is hidden, you may receive the following error message:

Run-time error '1004':

Protect method of Workbook class failed

