Click here to Skip to main content
15,908,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using following set of code to paste the values copied, But keep getting error messages..


xlWorkSheet1 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);


Excel.Range R1 = (Excel.Range)xlWorkSheet1.Cells[1, 1];


R1.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteValues,Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone,false,false);


Error :

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: PasteSpecial method of Range class failed

I tried to add R1.Select () , but it still failed..

Any help much appreciated
Posted

1 solution

Have you copied something on the clipboard before pasting.. You need to do a cut or copy.. The following code demonstrates that..

C#
Excel.Range R1 = (Excel.Range)oSheet.Cells[11, 11];
R1.Copy(Type.Missing);

Excel.Range R2 = (Excel.Range)oSheet.Cells[15, 15];
R2.PasteSpecial(Excel.XlPasteType.xlPasteValues,    Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);


The new code after your comments

C#
Excel.Range FilterRange = xlWorkSheet.get_Range("A1", "X6100");
       FilterRange.AutoFilter(6, "Project Leader", Excel.XlAutoFilterOperator.xlAnd, Type.Missing, true);
       Excel.Range splRange1 = FilterRange.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeVisible, Type.Missing);
       splRange1.Copy(Type.Missing);
       xlWorkSheet = (Excel.Worksheet) oWB.Worksheets.get_Item(2);
       Excel.Range R1 = (Excel.Range)xlWorkSheet.Cells[1, 1];
       R1.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteValues, Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
 
Share this answer
 
v2
Comments
VaTN 9-Jul-12 9:42am    
Yes I did copy before pasting .. Here the entire code which has copy part as well. Still have the issue explained earlier

FilterRange = xlWorkSheet.get_Range("A1", "BF800000");

FilterRange.AutoFilter(8,strValue , Excel.XlAutoFilterOperator.xlAnd, Type.Missing, true);

Excel.Range splRange1 = FilterRange.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeVisible,Type.Missing);

splRange1.Copy (Type.Missing);

xlWorkSheet1 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);


Excel.Range R1 = (Excel.Range)xlWorkSheet1.Cells[1, 1];

R1.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteValues,Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone,false,false);
Jim Jos 9-Jul-12 9:50am    
What is BF800000? ARe you trying to select the entire sheet ?
VaTN 9-Jul-12 9:55am    
Trying to filter the spreadsheet and then copy only filtered values
Jim Jos 9-Jul-12 10:02am    
OK.. you are trying to get the first worksheet is it the same worksheet you set the autofilter? If you are trying to copy and paste from the same worksheet it ll not work as it has filter on..
VaTN 9-Jul-12 10:36am    
Yes it was my bad that i was trying to paste in the same worksheet once I modify to paste in different worksheet it worked.

Thanks a lot for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900