Excel Interop - Cancel selection

∥☆過路亽.° 提交于 2019-12-31 02:42:07

问题


I'm copying and inserting rows in an Excel sheet, like so:

   while (rowsToAdd > 0)
   {
      // copy the existing row
      insertionCell.EntireRow.Copy(Type.Missing);

      // location of the new row
      Range newRow = insertionCell.EntireRow.get_Offset(1, 0).EntireRow;

      // insert the new row
      newRow.Insert(XlInsertShiftDirection.xlShiftDown, Type.Missing);

      rowsToAdd--;
   }

The problem I have is that sometimes, I'm left with a selection marquee around the row I originally copied.

Is there a way I can cancel the selection marquee (the way you'd normally do it with the Escape key?)


回答1:


In VBA it's Application.CutCopyMode = False




回答2:


Adding

myExcelApplication.CutCopyMode = XlCutCopyMode.xlCopy;

seems to do the trick, though the documentation does not explain it very well, and seems to be wrong, since bools are mentioned.




回答3:


I know it's pretty old, but as I had some issue copying one row to another, and as I got the proper solution on my case, I'm now glad to share it, just in case it could fix your problem (thinking about copy rows instead of inserting one).

Here is the solution:

Microsoft.Office.Interop.Excel.Range xlSourceRow;
Microsoft.Office.Interop.Excel.Range xlNewRow;
xlSourceRow.Copy(xlNewRow);

Using this solution prevents from getting any selection in your MS-Excel file.



来源:https://stackoverflow.com/questions/12380181/excel-interop-cancel-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!