ID: Q113786
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:
-and-
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/refguide/
To change the protection status of a workbook using a Visual Basic
subroutine contained in that workbook, use any of the following methods.
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
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
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
Microsoft has confirmed this to be a problem in the versions of Microsoft Excel listed at the beginning of this article.
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
For more information about the Protect Method or the Unprotect Method, choose the Search button in MS Excel Visual Basic Help (Macintosh) or the Visual Basic Reference (Windows), and type the following:
protection: workbook structure
Additional query words: 7.00 5.00 vba kbcode
Keywords : kbcode kbprg kbdtacode PgmOthr
Version : WINDOWS: 5.0, 5.0c, 7.0; MACINTOSH: 5.0, 5.0a
Platform : MACINTOSH WINDOWS
Issue type : kbbug
Last Reviewed: May 17, 1999