来自HRESULT的异常:0x800A03EC错误

Dan*_*lev 59 c# excel interop add-in exception

使用以下代码运行Excel加载项时出现"HRESULT:0x800A03EC"错误:

Excel.Range rng = ActiveSheet.Cells[x, y] as Excel.Range;                
string before = rng.Value2; 
string cleanV = System.Text.RegularExpressions.Regex.Replace(before, @"\s+", "");
rng.set_Value(cleanV);
Run Code Online (Sandbox Code Playgroud)

当错误发生时,X和Y设置为1,因此不会违反Excel范围.我进行了广泛的搜索并尝试了多种设置单元格值的方法(例如,Cells [x,y],range.set_Value()),但是为什么会发生这种错误以及如何避免它.

任何帮助是极大的赞赏.

以下是例外情况:


System.Runtime.InteropServices.COMException was unhandled by user code
  HResult=-2146827284
  Message=Exception from HRESULT: 0x800A03EC
  Source=""
  ErrorCode=-2146827284
  StackTrace:
       at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
       at Microsoft.Office.Interop.Excel.Range.set_Value(Object RangeValueDataType, Object value)
       at ImportValidation.ThisAddIn.removeAnySpaces(Int32 x, Int32 y) in c:\Users\dshevelev\Documents\Visual Studio 2012\Projects\ImportValidation\ImportValidation\ThisAddIn.cs:line 354
       at ImportValidation.ThisAddIn.ReadHeaders(Hashtable columnAddress) in c:\Users\dshevelev\Documents\Visual Studio 2012\Projects\ImportValidation\ImportValidation\ThisAddIn.cs:line 123
       at ImportValidation.ThisAddIn.mapColumns() in c:\Users\dshevelev\Documents\Visual Studio 2012\Projects\ImportValidation\ImportValidation\ThisAddIn.cs:line 493
       at ImportValidation.Ribbon1.button6_Click(Object sender, RibbonControlEventArgs e) in c:\Users\dshevelev\Documents\Visual Studio 2012\Projects\ImportValidation\ImportValidation\Ribbon1.cs:line 55
       at Microsoft.Office.Tools.Ribbon.RibbonPropertyStorage.ControlActionRaise(IRibbonControl control)
       at Microsoft.Office.Tools.Ribbon.RibbonPropertyStorage.ButtonClickCallback(RibbonComponentImpl component, Object[] args)
       at Microsoft.Office.Tools.Ribbon.RibbonManagerImpl.Invoke(RibbonComponentCallback callback, Object[] args)
       at Microsoft.Office.Tools.Ribbon.RibbonMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at Microsoft.Office.Tools.Ribbon.RibbonManagerImpl.System.Reflection.IReflect.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters)
  InnerException: 
Run Code Online (Sandbox Code Playgroud)

d1j*_*i1b 101

在这一行中得到了同样的错误

 Object temp = range.Cells[i][0].Value;
Run Code Online (Sandbox Code Playgroud)

解决了非零指数

 Object temp = range.Cells[i][1].Value;
Run Code Online (Sandbox Code Playgroud)

创建这个库的人怎么可能认为使用基于非零的索引是个好主意?

  • IMO,它可能与Excel的单元命名方案实际上是非零("R1C1")的事实有关...... (33认同)
  • 来自Excel的BASIC索引Excel早期(在1980年代)作为宏/脚本语言包括BASIC,后来导致Visual Basic for Applications.这是在C语言的广泛使用之前,它们具有更加数学上正确的基于零的索引,由于包括Java,Perl和Javascript在内的Web技术,它在1990年代得到了普遍使用. (23认同)

Gov*_*ert 16

这是一个常见但记录不清的Excel COM错误.我已经看到它记录为"NAME_NOT_FOUND",这意味着Excel的COM层被禁用,并且找不到COM属性或方法名称.

在Excel"忙"的情况下运行COM代码时,我会始终得到此错误,例如,如果您设置了启动代码的计时器,并且代码在用户编辑单元格或按下鼠标按钮时开始运行,则你总会得到这个错误.此错误仅在代码在主Excel线程上运行时发生,但似乎相当于错误VBA_E_IGNORE = 0x800AC472,这是从另一个线程调用Excel COM对象模型时获得的,而Excel是"忙".

唯一的解决方法似乎是重试(稍微延迟)COM调用,直到成功 - 当Excel不再"忙"时.


小智 8

检查您的起始索引.对于Microsoft.Office.Interop.Excel范围对象,它从1开始,而不是0.由于我的循环起始值,我收到了同样的错误.