Microsoft KB Archive/213903

From BetaArchive Wiki

Article ID: 213903

Article Last Modified on 11/23/2006



APPLIES TO

  • Microsoft Excel 2000 Standard Edition



This article was previously published under Q213903

SUMMARY

In Microsoft Excel 2000, unlike versions earlier than Excel 97, you cannot hide Microsoft Visual Basic for Applications modules. If you want to prevent users from seeing the Visual Basic code, you can lock the code in the workbook and thereby prevent users from viewing it. However, if you save the workbook as either an Excel 5.0/95 workbook or an Excel 97-2000 and 5.0/95 workbook, the module protection is lost when you open the workbook in Excel 5.0 or 7.0. Therefore, to use this method, you must maintain separate versions of the workbook for users of Excel 97 or 2000 and users of Excel 5.0 or 7.0.

This article describes a method of protecting Visual Basic code while allowing users of different versions of Excel to use the same version of your program. This method requires that you create the program in Excel 5.0 or 7.0 and then save it as an add-in file. This method hides the code in all versions of Excel. Any add-in code that you create in Excel 5.0 or 7.0 is not visible when the file is opened in Excel 2000.

MORE INFORMATION

In the example in this article, you compile a program into an add-in file. The interface for the program consists of two worksheets, which are copied into a new workbook when the add-in file is opened.

Note To create an add-in that is compatible with Excel 5.0 or 7.0 and Excel 97 or 2000, use the earliest version of Excel in which the add-in will be used. For example, if the workbook will be used with Excel 7.0 and Excel 2000, create the add-in in Excel 7.0.

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 a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

Creating the Source Workbook File for the Add-In

To create the workbook, follow these steps:

  1. In Excel 5.0 or 7.0, close and save any open workbooks, and then create a new workbook.
  2. If the new workbook does not contain at least two worksheets, insert a new worksheet. To do this, click Worksheet on the Insert menu.
  3. If the sheet tabs are not visible, click Options on the Tools menu, and then click the View tab. Under Window options, click Sheet tabs, and then click OK.
  4. Double-click the Sheet1 tab. In the Rename Sheet dialog box, type AddinSheet1 in the Name box, and then click OK.
  5. Double-click the Sheet2 tab. In the Rename Sheet dialog box, type AddinSheet2 in the Name box, and then click OK.
  6. On the Insert menu, point to Macro, and then click Module to insert a Visual Basic module sheet into the workbook.
  7. Type or paste the following code into the module sheet:

    ' This subroutine copies the worksheets to use as the interface
    ' for the program into a new workbook each time that the
    ' add-in is opened.
    
    Sub Auto_Open()
    
        Dim NewBook As Workbook
        Dim Ctr As Integer
    
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
    
        ' Create a new workbook.
        Set NewBook = Workbooks.Add
    
        ' Copy the two worksheets into the new workbook.
        ThisWorkbook.Sheets(Array("AddinSheet1", "AddinSheet2")).Copy _
        before:=NewBook.Sheets(1)
    
        ' Delete all of the other sheets in the new workbook. The
        ' initial value of the counter is 1 greater than the number of
        ' worksheets that you want to copy into the new workbook.
        For Ctr = 3 To NewBook.Sheets.Count
    
           NewBook.Sheets(3).Delete
    
        Next
    
    End Sub
    
    ' This sample demonstrates that any buttons that you place on the
    ' interface worksheets are still functional when the worksheets are
    ' copied into a new workbook.
    Sub Test()
    
        MsgBox "This is a test"
    
    End Sub
                        
  8. Click AddinSheet1.
  9. On the View menu, click Toolbars. In the Toolbars list, click Forms, and then click OK.
  10. Draw a button on the worksheet by using the Create Button tool.
  11. In the Assign Macro dialog box, click Test in the list of available macros, and then click OK.
  12. On the File menu, click Save. In the File name box, type Test.xls, and then click Save.

Creating the Add-In File

To create the add-in file, follow these steps:

  1. Switch to Module1.
  2. On the Tools menu, click Make Add-In. In the File name box, type Test.xla, and then click Save.
  3. Close Test.xls. You should have two files, Test.xls and Test.xla. The add-in file, Test.xla, is the file that you must distribute to your users. Microsoft recommends that you keep a copy of Test.xls because you need it to update the add-in.


When you open Test.xla, a new workbook is created with two worksheets that are exact copies of the worksheets in the add-in file. If you click the button on AddinSheet1, the Test subroutine in the add-in file runs.


REFERENCES

For more information about how to insert an add-in, click Microsoft Excel Help on the Help menu, type load an installed add-in program in microsoft excel in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:

212536 OFF2000: How to Run Sample Code from Knowledge Base Articles



Additional query words: secure securing protect XL2000

Keywords: kbhowto kbinfo kbmacro kbprogramming kbui KB213903