2014-03-06 24 views
1

Alrighty球員我對你而言更喜歡腦力激盪;我正在嘗試開發一個相對簡單的excel插件,該插件應該從工作簿中的表A中讀取數據,並在按下按鈕時創建一個新的或更新的表B,以在工作表A中包含所述數據的簡化版本。下面是一些示例代碼,我的工作:C#Excel加載項工作表錯誤0x800A03EC - 未請求的單元格移動

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 we want to move from is same name as book name 
    if (sheet.Name == wb.Name) 
     sheetA = sheet; 

    // Sheet to move to will be called review. Clean if it exists. 
    else if (sheet.Name == "Review") 
    { 
     sheetB = sheet; 
     sheetB.Cells.ClearContents(); 
    } 
} 

// If origin sheet cannot be found, assume it's the first sheet 
if (sheetA == null) 
    sheetA = (Excel.Worksheet)wb.Worksheets[1]; 

// Add the review sheet after the origin sheet if it doesn't exist 
if (sheetB == null) 
{ 
    sheetB = wb.Worksheets.Add(After: sheetA); 
    sheetB.Name = "Review"; 
} 

// Simply copy across the value of the first cell 
sheetB.Range["A1"].Value2 = sheetA.Range["A1"].Value2; 

下面這段代碼的結果似乎是取決於什麼是「編輯模式」完全不同的(細胞正在編輯)或沒有。如果不是,一切都會如你所期望的那樣,在正確的位置創建新的工作表並填充單元格。

如果正在編輯單元格,編輯的單元格將移動到工作簿中的另一個工作表。如果沒有其他工作表移動到COMExceptionHRESULT: 0x800A03EC被拋出(錯誤未知)。

這個錯誤顯示了很多,它真的令人沮喪,它基本上告訴你「如果我知道該死」,所以任何想法將不勝感激。最常見的事情似乎是「工作表不存在」,這將是這種情況,但我不知道爲什麼它想要將編輯的單元格放在第一位?

回答

1

找到解決方案。模擬返回鍵擊(我的示例的第一行)確實退出編輯模式,但是應用程序需要延遲來處理它。我的執行計劃如下:

Application.SendKeys("{ENTER}"); // Exit edit mode 
Timer timer = new Timer(100); // 100ms delay 
timer.AutoReset = false; // Stop the timer looping and re-executing 
timer.Elapsed += new ElapsedEventHandler((Sender, ent) => 
{ 
    // Code you want to execute outside of edit mode 
}); 
timer.Start(); // Start her up! 

希望能幫助一些失去遊魂的人!

相關問題