The information in this article applies to:
SYMPTOMSIn 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- WORKAROUNDMicrosoft 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/overview/overview.aspTo change the protection status of a workbook using a Visual Basic subroutine contained in that workbook, use any of the following methods. Method 1Use 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.
Method 2Unhide 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:
Method 3Set 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:
STATUSMicrosoft has confirmed this to be a problem in the versions of Microsoft Excel listed at the beginning of this article. MORE INFORMATIONWhen 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:
REFERENCESFor 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: vba kbcode
Keywords : kbcode kbprg kbdtacode PgmOthr |
Last Reviewed: November 9, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |