HOW TO

Lock the Visual Basic Editor with a password

Last Published: April 25, 2020

Summary

ICustomizationFilter can be used to prevent end users from opening the Visual Basic Editor and viewing or editing the code a document.

Procedure

The following example locks the Visual Basic Editor from being opened in an MXD document. Upon trying to open the Editor, the user will be prompted to enter the appropriate password.

  1. Start ArcMap.
  2. Open the Visual Basic Editor.

    In ArcMap, select Tools > Macros > Visual Basic Editor.

  3. In the Project Explorer window, expand Project.mxt and select ArcMap Objects > ThisDocument. Right-click and select View Code.
    [O-Image] Visual Basic Project  Explorer
    Note:
    Code in the ThisDocument code module will only run in the current map document. To store the code in all the map documents, open the Normal.mxt ThisDocument code module.

  4. Copy the following code into the code module.

    Code:
    Private Function MxDocument_OpenDocument() As Boolean
    'Do error checking in case there is already a lock active.
    On Error GoTo lockErr

    'Create an instance of the custom customization filter, MyFilter
    Dim m_CustomFilter As ICustomizationFilter
    Set m_CustomFilter = New MyFilter

    'Lock customization using MyFilter.
    'In this example, "MYLOCK" is the password.
    Application.LockCustomization "mylock", m_CustomFilter
    Exit Function

    lockErr:
    MsgBox "There is already an active lock.", , "Lock Error"
    End Function

  5. From the Editor menu, click Insert > Class Module. Copy the following code into the new module:

    Code:
    'MyFilter.cls
    Option Explicit
    Implements ICustomizationFilter

    Private Function ICustomizationFilter_OnCustomizationEvent _
    (ByVal custEventType As esriCore.esriCustomizationEvent, _
    ByVal eventCtx As Variant) As Boolean
    If custEventType = esriCEShowCustDlg Then
    ICustomizationFilter_OnCustomizationEvent = True
    Exit Function
    End If

    ' Lock the Visual Basic editor
    ' custEventType is esriCEShowVBAIDE
    ' eventCtx is nothing
    If custEventType = esriCEShowVBAIDE Then
    ICustomizationFilter_OnCustomizationEvent = True
    End If
    End Function

  6. In the Properties window, change the Name property from 'Class1' to 'MyFilter'.
  7. Save the MXD and exit ArcMap.
  8. Start ArcMap and open the MXD.
  9. Open the Visual Basic Editor. A dialog box will be presented asking for the password 'MYLOCK' to be entered.

Article ID:000004891

Software:
  • ArcMap 8 x
  • ArcMap 9 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic