Click here to Skip to main content
15,909,193 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Why does the following simple code throw an exception in the last line and what can I do against it?

Clipboard.SetData(DataFormats.Text, "<Script");

Microsoft.Office.Interop.Excel.Application oApplication = new Microsoft.Office.Interop.Excel.Application();
oApplication.Visible = true;

Workbooks oWorkbooks = oApplication.Workbooks;

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
Workbook oWorkbook = oWorkbooks.Add();

Worksheet rWorksheet = (Worksheet)oWorkbook.ActiveSheet;

Range rRange = rWorksheet.Range[rWorksheet.Cells[1, 1], rWorksheet.Cells[1, 1]];
rRange.PasteSpecial(XlPasteType.xlPasteValues);


Edit: A COMException with Message "PasteSpecial method of Range class failed", HResult 0x800A03EC and HelpLink "xlmain11.chm" is being thrown.
Posted
Updated 17-Jan-16 19:04pm
v3
Comments
Jochen Arndt 15-Jan-16 6:47am    
Is there no error message text provided by the exception?
If so, you should add it to your question.
Member 2654753 18-Jan-16 1:08am    
Sorry, added info about the exception.
Maciej Los 15-Jan-16 17:35pm    
What's error message?
Jochen Arndt 18-Jan-16 3:39am    
This may be sourced by many reasons where the ones listed below did not seem to apply in your case. But you may check them:
- There is nothing on the clipboard.
- The amount of text to be pasted is too large (depending on the Excel version the limit is somewhere between 6 and 10 KB).
- The range is invalid.
- The cell has a data format set and the pasted data can't be converted to that format.
- The sheet or workbook is protected.
- And many others reasons.

Especially a variation of the data format may apply here because your text begins with < which may be interpreted as formula by Excel but it is an invalid one. You might check this by pasting other text.
Member 2654753 18-Jan-16 7:11am    
Thanks for your reply. While the reasons 1-3 and 5 do not seem to apply, it may have to do something with formats, since using XlPasteType.xlPasteAll instead of XlPasteType.xlPasteValues throws no exception.

Pasting with XlPasteType.xlPasteAll sets the rRange.NumberFormat to "General", but pasting with XlPasteType.xlPasteValues fails even if I explicitly set the NumberFormat to "General" before the paste (which is not surprising, because "General" seems to be the standard). So I am still lost.

Background: If I have the text "<Script" in the clipboard, I cannot even manually paste this text into excel without first entering the cell-mode (i.e. without first pressing F2). I was hoping to convince excel by setting the NumberFormat to "Text" ("@") and then pasting with XlPasteType.xlPasteValues so that the NumberFormat stays "Text". But pasting with XlPasteType.xlPasteValues always seems to fail, even with text like "abc" in the clipboard.

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