XL97: Controlling Events in an Automation Server

ID: Q184000

The information in this article applies to:

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

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:

1. Start Microsoft Excel, close all open workbooks and open a new workbook.

2. Start the Visual Basic Editor.

3. Insert a new module into the project and enter the following code in

   the module:

      Sub Disable_Events()
         Application.EnableEvents = False
      End Sub

4. In the Project Explorer, double-click ThisWorkbook.

   This step displays the code module for the workbook.

5. Enter the following code for the SheetDeactivate event of the workbook:

      Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
         MsgBox Sh.Name
      End Sub

6. Quit the Visual Basic Editor and return to Microsoft Excel.

7. Select the sheet tab for any sheet other than the active sheet.

   A message box displays the name of the sheet that was deactivated.

8. Close and save this workbook as Test.xls on the root directory of drive
   C.

9. Quit Microsoft Excel.

Creating the Automation Macro

To create this macro, follow these steps:

1. Start Microsoft Word 97, close all open documents, and then open a new

   document.

2. Start the Visual Basic Editor.

3. 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

4. Run the Automation_Example macro.

   Excel is started and the Test.xls workbook is opened.

5. Select any sheet tab other than the active sheet.

   The message box is displayed because events were not disabled by
   EnableEvents property.

6. Quit Microsoft Excel.

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

8. Run the Automation_Example macro.

9. 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.

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.

STATUS

This behavior is by design of Microsoft Excel

REFERENCES

For more information about the EnableEvents property, from the Visual Basic Editor, click the Office Assistant, type "enableevents," click Search, and then click to view "EnableEvents Property."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If the Assistant is not able to answer your query, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q176476
   TITLE     : OFF: Office Assistant Not Answering Visual Basic Questions

Additional query words: XL97 ole
Keywords          : kbdta 
Version           : WINDOWS:97
Platform          : WINDOWS
Issue type        : kbprb

Last Reviewed: May 17, 1999