C# Excel add-in worksheet error 0x800A03EC - unrequested cell movement -


alrighty guys have rather brain mangler you; i'm trying develop relatively simple add-in excel should read in data sheet in workbook, create new or update sheet b contain simplified version of said data in sheet @ press of button. below example code i'm working on:

application.sendkeys("{enter}"); // exit edit mode excel.workbook wb = this.application.activeworkbook; excel.worksheet sheeta = null; excel.worksheet sheetb = null;  foreach (excel.worksheet sheet in wb.worksheets) {     // assume origin sheet want move same name book name     if (sheet.name == wb.name)         sheeta = sheet;      // sheet move called review. clean if exists.     else if (sheet.name == "review")     {         sheetb = sheet;         sheetb.cells.clearcontents();     } }  // if origin sheet cannot found, assume it's first sheet if (sheeta == null)     sheeta = (excel.worksheet)wb.worksheets[1];  // add review sheet after origin sheet if doesn't exist if (sheetb == null) {     sheetb = wb.worksheets.add(after: sheeta);     sheetb.name = "review"; }  // copy across value of first cell sheetb.range["a1"].value2 = sheeta.range["a1"].value2; 

now outcomes of code seem radically different depending on whether in "edit mode" (cells being edited) or not. if not, you'd expect, new sheet created in correct position , cell populated.

if cell being edited though, edited cell moved worksheet in workbook. if there no other sheet move comexception hresult: 0x800a03ec thrown (error unknown).

this error shows lot , it's frustrating telling "be damned if know", ideas appreciated. common thing seems "worksheet doesn't exist" case here, can't tell why wants move edited cell in first place?

solution found. simulating return key stroke (first line of example) exit edit mode, delay required application process it. implementation worked out as:

application.sendkeys("{enter}"); // exit edit mode timer timer = new timer(100); // 100ms delay timer.autoreset = false; // stop timer looping , re-executing timer.elapsed += new elapsedeventhandler((sender, ent) => {     // code want execute outside of edit mode }); timer.start(); // start up! 

hope helps lost wandering soul!


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -