Microsoft KB Archive/824007

= PRB: ActiveX Control Events Are Not Handled When the Excel Macro Security Level Is Set to High =

Article ID: 824007

Article Last Modified on 1/5/2007

-

APPLIES TO


 * Microsoft Office Excel 2003
 * Microsoft Visual Studio Tools for the Microsoft Office System version 2003

-



SYMPTOMS
When you start Microsoft Office Excel, and you open a workbook that uses a managed code extension, you may receive a message similar to the following:

Macros are disabled because the security level is set to High and a digitally signed Trusted Certificate is not attached to the macros. To run the macros, change the security level to a lower setting (not recommended), or request the macros be signed by the author using a certificate issued by a Certificate Authority.

The workbook then opens in Design Mode, and any managed code event handlers for Microsoft ActiveX controls in the workbook will not run.



CAUSE
This behavior occurs when the following are true:
 * The macro security level in Excel is set to High.

-and-
 * The workbook contains a Microsoft Visual Basic for Applications (VBA) project that has not been digitally signed.

When the macro security level is set to High, any workbook that contains a VBA project that has not been digitally signed must open in Design Mode.



WORKAROUND
To work around this behavior, use one of the following methods.

Method 1: Add a Digital Signature to the Macro
Excel runs macros that are signed by a trusted source. Obtain a digital signature, and then add the digital signature to the macro. To add the digital signature:
 * 1) Open the file that contains the macro project that you want to sign.
 * 2) On the Tools menu, point to Macro, and then click Visual Basic Editor.
 * 3) In Project Explorer, click the project that you want to sign.
 * 4) On the Tools menu, click Digital Signature.
 * 5) Click Choose, click the certificate, and then click OK. Click OK again to close the Digital Certificate dialog box.

Method 2: Change the Macro Security Level
To change the macro security level:
 * 1) On the Tools menu, click Options.
 * 2) Click the Security tab.
 * 3) Under Macro Security, click Macro Security, and then click the Security Level tab.
 * 4) On the Security Level tab, click the security level that you want to use, and then click OK two times.

For more information about macro security, click Microsoft Excel Help on the Help menu. Type about macro security in the Search text box of Assistance. Click Search.

Method 3: Remove the VBA Code from the Workbook
If the functionality that your VBA code provides has a managed code equivalent, you can transition this functionality to the managed code extension. After you have transitioned the code, you can remove all the modules and code that the workbook's VBA project contains.

When you use this method, you avoid the security warning and the workbook does not open in Design Mode when the Excel macro security level is set to High. If you cannot completely remove the VBA project from your workbook, you can move only the worksheets in the workbook to a new workbook.



Steps to Reproduce the Behavior
 Create a new Excel workbook project:  Start Visual Studio .NET 2003. On the File menu, click New, and then click Project. Under Microsoft Office System Projects, click Visual Basic Projects, and then click Excel Workbook. Click OK to start the Microsoft Office Project Wizard. Click Create new document, and then click Finish.</li>  In the code window, locate the following code: ' Called when the workbook is opened. Private Sub ThisWorkbook_Open Handles ThisWorkbook.Open

End Sub Replace this code with the following code: ' Called when the workbook is opened. Friend WithEvents btn As MSForms.CommandButton Private Sub ThisWorkbook_Open Handles ThisWorkbook.Open MessageBox.Show(&quot;The Open Event Fired.&quot;) btn = Me.FindControl(&quot;CommandButton1&quot;) End Sub </li>  In the code window, paste the following code in the class module: Private Sub btn_Click Handles btn.Click MessageBox.Show(&quot;Click&quot;) End Sub </li> Press F5 to build and then run the project.

The workbook opens in Excel and a message box appears. Click OK to close the message box.</li></ol> </li> In Excel, point to Toolbars on the View menu, and then click Control Toolbox.</li> Add a Command Button to the workbook.</li> On the Tools menu, point to Macro, and then click Security.</li> Click the Security Level tab, click High, and then click OK.</li> Click Tools, point to Macro, and then click Visual Basic Editor.</li> On the Insert menu, click Module.</li>  Paste the following code in the module: Sub a MsgBox &quot;Hello&quot; End Sub </li> On the File menu, click Close and Return to Microsoft Office Excel.</li> Save your changes to the workbook, and then quit Excel.</li> Restart Excel, and then open the workbook.

The security message appears.</li> Click OK to close the message box.

The message box from the Open event handler appears.</li> Click OK to close the message box.

The workbook opens in Design Mode.</li> Click CommandButton1.

Notice that the managed code event handler does not run.</li></ol>

<div class="references_section">