XL: Protect Method Fails When Module Is Hidden

Last reviewed: December 2, 1997
Article ID: Q113786

The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for the Macintosh, versions 5.0, 5.0a
  • Microsoft Excel for Windows 95, version 7.0

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, 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 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 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/default.asp

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.

            The following code should be entered 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 above. We are researching this problem, and will post new information here in the Microsoft Knowledge Base as it becomes available.

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

REFERENCES

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
Keywords : kbcode kbprg PgmOthr
Version : WINDOWS: 5.0, 5.0c, 7.0; MACINTOSH: 5.0, 5.0a
Platform : MACINTOSH WINDOWS
Issue type : kbbug
Solution Type : kbworkaround


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: December 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.