如何解决此错误"Range class的Select方法失败". [英] How to solve this error "Select method of Range class failed"

查看:160
本文介绍了如何解决此错误"Range class的Select方法失败".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已编写此代码以从Excel数据库中删除行,但是当我运行此代码时,出现上述错误.请帮助解决此问题.
这是我的代码:

I have written this code to delete row from Excel database but when i am running this i get the above error. Please help to to solve this.
Here is my code:

if (MessageBox.Show("Do you realy want to delete this data?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (index != -1)
                    {
                        ex.Application xlApp;
                        ex.Workbook xlWorkBook;
                        ex.Worksheet xlWorkSheet;
                        object misValue = System.Reflection.Missing.Value;

                        xlApp = new ex.ApplicationClass();
                        xlWorkBook = xlApp.Workbooks.Open(Path.GetDirectoryName(Application.ExecutablePath) + "\\Taas SMS.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

                        if (TaasSMS.AppCode.GenModule.Type == "Member")
                        {
                            xlWorkSheet = (ex.Worksheet)xlWorkBook.Worksheets.get_Item(2);
                            ex.Range range;
                            range = (ex.Range)xlWorkSheet.Rows[index + 2, Type.Missing];
                            range.Select();
                            range.Delete(ex.XlDirection.xlUp);
                            xlWorkBook.SaveAs(Path.GetDirectoryName(Application.ExecutablePath) + "\\Temp\\Taas SMS.xls", ex.XlFileFormat.xlExcel8, Type.Missing, Type.Missing, Type.Missing, Type.Missing, ex.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                            xlWorkBook.Close(true, misValue, misValue);
                            xlApp.Quit();

                            releaseObject(xlWorkSheet);
                            releaseObject(xlWorkBook);
                            releaseObject(xlApp);
                        }
                        else if (TaasSMS.AppCode.GenModule.Type == "Media")
                        {
                            xlWorkSheet = (ex.Worksheet)xlWorkBook.Worksheets.get_Item(3);
                            ex.Range range;
                            range = (ex.Range)xlWorkSheet.Rows[index + 2, Type.Missing];
                            range.Select();
                            range.Delete(ex.XlDirection.xlUp);
                            xlWorkBook.SaveAs(Path.GetDirectoryName(Application.ExecutablePath) + "\\Temp\\Taas SMS.xls", ex.XlFileFormat.xlExcel8, Type.Missing, Type.Missing, Type.Missing, Type.Missing, ex.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                            xlWorkBook.Close(true, misValue, misValue);
                            xlApp.Quit();

                            releaseObject(xlWorkSheet);
                            releaseObject(xlWorkBook);
                            releaseObject(xlApp);
                        }
                        index = -1;
                        string sourcePath2 = Path.GetDirectoryName(Application.ExecutablePath) + "\\Temp\\Taas SMS.xls";
                        string DestinationPath2 = Path.GetDirectoryName(Application.ExecutablePath) + "\\Taas SMS.xls";
                        File.Delete(DestinationPath2);
                        File.Move(sourcePath2, DestinationPath2);
                        FillGrid();
                        MessageBox.Show("Data Deleted!...");
                    }





at

range.Select();

处,我收到上述错误:未处理COM异常(Range类的选择方法失败)

i get that above error: COM exception was unhandled (Select method of Range class failed)

推荐答案

您无法在工作表上选择一个单元格那不是活动工作表.

只需注意,您应该能够调试并转到引发此错误的行.
您的代码段中提供了很多代码,但是要在没有实际调试的情况下遍历每一行几乎是不可能的.
You can''t select a cell on a sheet that is not the active sheet.

Just as a note, you should be able to debug and get to the line that is throwing this error.
You have a provided a lot of code in your snippet, but it is close to impossible to go through every line without actually debugging.


如果没有真的好
我的猜测是您要选择的行没有任何数据,因此Excel认为它在可用工作表之外(可能是因为您正在查找的工作表并非您想要的).但是从外部调试像这样的代码几乎是不可能的.
If there is not a really good reason to be using Excel as a primary data source, you should use a database. Excel isn''t designed to be one, and requiring the user to have Office (a non free product) to run your app seems silly when you could use a free database (mySQL, SQL Server Express, etc).

My guess is that the row you are trying to select doesn''t have any data so Excel thinks it''s outside the available worksheet (possibly because you are looking in a worksheet that isn''t the one you meant to). But it is pretty much impossible to debug code like that from outside.


这篇关于如何解决此错误"Range class的Select方法失败".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆