下面这段代码会抛出这个异常。
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.Office.Tools.Excel;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
namespace ExcelWorkbook6
{
public partial class ThisWorkbook
{
private void ThisWorkbook_Startup(object sender, System.EventArgs e)
{
try
{
Excel.Range objRage = Application.ActiveCell;
string formula1 = @"=A5/0";
objRage.Validation.Delete();
objRage.Validation.Add(Excel.XlDVType.xlValidateList, Excel.XlDVAlertStyle.xlValidAlertStop, Type.Missing, formula1, Type.Missing);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void ThisWorkbook_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisWorkbook_Startup);
this.Shutdown += new System.EventHandler(ThisWorkbook_Shutdown);
}
#endregion
}
}
原因是0作了除数。在试验其他函数时只要函数出错都能抛出这个异常。所以,当你看到这个异常时就要检查一下自己定义的公式是否正确了。