Microsoft KB Archive/184000

= XL97: Controlling Events in an Automation Server =

Article ID: 184000

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition

-



This article was previously published under Q184000





SYMPTOMS
When Microsoft Excel is being used as an Automation server during an Automation session, the EnableEvents property may appear to not work. Attempts by the client application to set this property to FALSE through code in the client application are ignored.



CAUSE
This behavior happens because, during Automation, each line of code that is sent to Microsoft Excel to be run from an Automation client is treated as a separate Microsoft Excel macro. The EnableEvents property would be turned off (set to False) only for that one line of code and would be automatically turned back on (set to True) for the next line of code that is sent to Microsoft Excel. Therefore, using the EnableEvents property in this context is not effective.



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 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:

https://partner.microsoft.com/global/30000104

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

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

To set the EnableEvents property to False during an Automation session, use the Run method from the Automation client to run an Excel macro that sets the property to False.

The following example demonstrates how to disable events in an Automation session that controls Microsoft Excel

Creating the Excel Macro
To create the macro, follow these steps:

 Start Microsoft Excel, close all open workbooks and open a new workbook. Start the Visual Basic Editor.  Insert a new module into the project and enter the following code in the module: Sub Disable_Events Application.EnableEvents = False End Sub  In the Project Explorer, double-click ThisWorkbook.

This step displays the code module for the workbook.  Enter the following code for the SheetDeactivate event of the workbook: Private Sub Workbook_SheetDeactivate(ByVal Sh As Object) MsgBox Sh.Name End Sub  Quit the Visual Basic Editor and return to Microsoft Excel.</li> Select the sheet tab for any sheet other than the active sheet.

A message box displays the name of the sheet that was deactivated.</li> Close and save this workbook as Test.xls on the root directory of drive C.</li> Quit Microsoft Excel.</li></ol>

Creating the Automation Macro
To create this macro, follow these steps:

<ol> Start Microsoft Word 97, close all open documents, and then open a new document.</li> Start the Visual Basic Editor.</li>  Insert a new module in the project and enter the following code in the module: Sub Automation_Example

Dim xlobj As Object Set xlobj = CreateObject("excel.application")

xlobj.Visible = True xlobj.EnableEvents = False xlobj.Workbooks.Open FileName:="c:\Test.xls" 'xlobj.Run "Disable_Events"

Set xlobj = Nothing

End Sub </li> Run the Automation_Example macro.

Excel is started and the Test.xls workbook is opened.</li> Select any sheet tab other than the active sheet.

The message box is displayed because events were not disabled by EnableEvents property.</li> Quit Microsoft Excel.</li>  Change the Automation_Example macro to the following: Sub Automation_Example

Dim xlobj As Object Set xlobj = CreateObject("excel.application")

xlobj.Visible = True 'xlobj.EnableEvents = False xlobj.Workbooks.Open FileName:="c:\Test.xls" xlobj.Run "Disable_Events"

Set xlobj = Nothing

End Sub </li> Run the Automation_Example macro.</li> Select any sheet tab other than the active sheet.

The message box is not displayed because events were disabled by the Disable_Events macro in Test.xls.</li></ol>

NOTE: If the Test.xls workbook had contained a Workbook_Open event, it would not have been disabled because the Disable_Events macro in the workbook would have run after the workbook was opened. A workaround to this scenario is to use an Auto_Open macro in the workbook instead of a Workbook_Open event macro. Because Auto_Open macros are not automatically executed when a workbook is opened by running a procedure, the macro does not run in this scenario.

<div class="status_section">

STATUS
This behavior is by design of Microsoft Excel

<div class="references_section">