Microsoft KB Archive/823988

= BUG: You cannot delete an Excel list row from the CommandButton Click event =

Article ID: 823988

Article Last Modified on 1/15/2007

-

APPLIES TO


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

-





SYMPTOMS
You have built a managed code extension for Office Excel 2003. Your solution uses the Click event of an MSForms CommandButton control to remove rows from a List object that is located on a worksheet. When you click the button to remove the List row, the row is not removed, and you may receive a run-time error.



STATUS
Microsoft has confirmed that this is a bug in Office Excel 2003.



RESOLUTION
To resolve this problem, make sure that CommandButton does not have the focus when you delete the list row. Use one of the following methods:
 * Set the TakeFocusOnClick property of CommandButton to false.

-or-
 * Re-select the current worksheet selection to take the focus away from CommandButton.



WORKAROUND
To work around this problem, use one of the following methods:   Set the TakeFocusOnClick property for the CommandButton control to false : protected void ThisWorkbook_Open {   //Get a reference to the first worksheet. ws = (Excel.Worksheet)(ThisWorkbook.Worksheets[1]);

//Set up the Click event handler for CommandButton1. cb = (MSForms.CommandButton)(this.FindControl(&quot;CommandButton1&quot;)); cb.Click+= new MSForms.CommandButtonEvents_ClickEventHandler(cbClick);

cb.TakeFocusOnClick = false; }

-or-   Call the Select method for the current worksheet Selection to remove focus from the CommandButton: private void cbClick {    ThisApplication.Selection.GetType.InvokeMember(&quot;Select&quot;,        System.Reflection.BindingFlags.Public |         System.Reflection.BindingFlags.InvokeMethod |         System.Reflection.BindingFlags.Instance,        null,        ThisApplication.Selection,        null); ws.ListObjects[1].ListRows[1].Delete; } 



Steps to Reproduce the Behavior
 Start Microsoft Visual Studio .NET 2003. On the File menu, point to New, and then click Project.</li> In the list of project types, expand Microsoft Office System Projects, and then click Visual C# Projects. Select Excel Workbook in the template list, and then click OK.</li> Click Finish in the Microsoft Office Project Wizard.</li> Press the F5 key to run the project and to open the workbook in Excel.</li> Modify the workbook as follows: <ol style="list-style-type: lower-alpha;"> Select cell A1.</li> On the Data menu, point to List, and then click Create List. The Create List dialog appears. Click OK.</li> Add any text to cells A2, A3 and A4 (A2:A4).</li> On the View menu, point to Toolbars, and then click Control Toolbox.</li> Draw a CommandButton control on the worksheet. The default name is CommandButton1.</li> Save the workbook, and then quit Excel.</li></ol> </li>  Add the following code to the Thisworkbook.cs code module: private MSForms.CommandButton cb; private Excel.Worksheet ws;

protected void ThisWorkbook_Open {   //Get a reference to the first worksheet. ws = (Excel.Worksheet)(ThisWorkbook.Worksheets[1]);

//Set up the Click event handler for CommandButton1. cb = (MSForms.CommandButton)(this.FindControl(&quot;CommandButton1&quot;)); cb.Click+= new MSForms.CommandButtonEvents_ClickEventHandler(cbClick); }

private void cbClick {   try {       //Delete the first row in the list object. ws.ListObjects[1].ListRows[1].Delete; }   catch (Exception ex) {       System.Diagnostics.Debug.WriteLine(ex.Message); MessageBox.Show(ex.Message); } } </li> Press the F5 key to build and to run the project.</li> Click CommandButton1.

Result An exception is caught when deleting the row in the list. You receive the following error message

Exception from HRESULT: 0x800A03EC

</li></ol>

Keywords: kbbug kbautomation kbnofix KB823988

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.