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 Microsoft Advisory Services. For more information, visit these Microsoft Web sites:
Microsoft Certified Partners -
https://partner.microsoft.com/global/30000104Microsoft Advisory Services -
http://support.microsoft.com/gp/advisoryserviceFor 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 Excel.
Creating the Excel Macro
To create the macro, follow these steps:
- Start Microsoft Excel, close all open workbooks, and then open a new workbook.
- Start the Visual Basic Editor (ALT+F11).
- On the Insert menu, click Module. In the new module, enter the following code:
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.
- Select the sheet tab for any sheet other than the active sheet.
A message box displays the name of the sheet that was deactivated.
- Close and save this workbook as Test.xls on the root directory of drive C.
- Quit Microsoft Excel.
Creating the Automation Macro
To create this macro, follow these steps:
- Start Microsoft Word 2000, close all open documents, and then open a new document.
- Start the Visual Basic Editor.
- 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
- Run the Automation_Example macro.
Excel is started and the Test.xls workbook is opened.
- Select any sheet tab other than the active sheet.
The message box is displayed because events were not disabled by
EnableEvents property.
- Quit Excel.
- 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
- Run the Automation_Example macro.
- 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
run when a workbook is opened by running a procedure, the macro does
not run in this scenario.