Range.PastSpecial(...) is failing - c#

I am using Interop.Excel for doing this task.
I am trying to copy a range of cells from one workbook's sheet and paste it into a separate workbook's sheet, but I want to keep the formatting from the source sheet and paste it into the destination sheet. I found the PasteSpecial, and tried to use that, but when I try it, I get an unhelpful exception message : "PastSpecial methos of Range class failed", which has an HResult of -2146827284. Could not find anything via google, so here I am to hopefully get some help...
I use the code below to try this. I also did try using Excel.XlPasteType.xlPasteAll as the initial parameter to the PastSpecial method, and that did actually Paste the data into the destination sheet, and so I thought great, this works, but when I looked closer, I could see, very fiently, what looked like extra gridlines, and when I clicked on the pasted data, it was a whole object which could be moved around, much like an image pasted in that can be moved. This of course is not what I wanted. The idea is that I need to preserve the formatting and the border styles and the colors used.
Many thanks for reading - apologies if my formatting in the post has messed up.
Excel.Range rng6 = sht6.Cells.get_Range("A1", "O55");
Excel.Range rng1 = sht.get_Range("A" + rowStart6.ToString(), "O" + rowEnd6.ToString());
rng6.Copy(mis);
/** Fails here at PasteSpecial**/
rng1.PasteSpecial(Excel.XlPasteType.xlPasteFormats, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);

Related

EPPlus returns #VALUE! instead of Value from the cell content randomly [epplus]

As per Current requirement I need to read value from the cell using EPPlus. This cell contains the formula and showing value correctly in XL Sheet. but when i am reading that value using EPPlus some cells are giving correct value but some cells are giving error "#VALUE!". I have used ".Calculate()" before read the value but still facing the same problem. Please find the code below in c#.
totalRecycleWorksheet.Cells[row, colval + 5].Style.Numberformat.Format = "#";
totalRecycleWorksheet.Cells[row, colval + 5].Calculate();
var value = totalRecycleWorksheet.Cells[row, colval + 5].Value;
if (!totalRecycleWorksheet.Cells[row, colval + 5].Value.ToString().Equals("#VALUE!")) {}
and here is the formula in every cells:
=IF(('Failure Item'!E348+ROUNDUP(('Output'!E348)*$B$1,0)-'Purchased Items'!F348)>0,('Failure Item'!E348+ROUNDUP(('Output'!E348)*$B$1,0)-'Purchased Items'!F348),0)
and values are as per the screenshot:
Also you can check the Output I have stored in datatable to check the value:
The only examples I see call Calculate at the workbook level such as
excelPackage.Workbook.Calculate();
I had a similar problem. In my case the Excel workbook was a macro-enabled (.xlsm) file. It was macro enabled because I had made use of VBA functions.
When reading Excel.Range.Value2 property from cells the numerical result was consistently -2146826273. I searched this error code as the Hex (800A 07DF) with no luck, but eventually used a bit of debugging to find it resulted from Excel outputting #VALUE! in the cell I was trying to read.
This was because the macros weren't enabled when I'd loaded it via C#, so calls to the VBA functions were failing.
I followed the advice in: Programmatically enable Excel macro in C#
to enable macros on the workbook and all my #VALUE! problems disappeared.

Embed file in excel worksheet/cell

I hope someone can help me. Is there a way to embed a specific file (.txt) into an excel cell? I'm currently using epplus, and I would like to embed programmatically a file into a specific excel cell. I did manage to add a hyperlink, but my goal is to have it embedded.
Worksheet.Cells[rowNumber, colNumber].Value = ....
Is there any way to do it? I couldn't find anything online.
As mentioned in the comments, you can certainly put text within a cell, but bear in mind Excel does have a limit to the number of characters it will allow in a single cell. It's pretty large, but conceivably the contents of a text file could exceed that limit -- even if future versions of Excel keep increasing what the limit is (as they have in the past).
You can also embed an OLE object in your worksheet, and a text file qualifies for that. I don't know that you can assign it to a cell, per se. You can change the location, shape and behavior to fit in a cell and behave as though it's part of a cell, but I don't know that it ever belongs to a range the way formulas do. I could be wrong.
The basic construct of how to embed an OLE object into a worksheet is as follows:
Excel.OLEObject ole = ws.OLEObjects().Add(Filename: #"C:\Users\hambone\Documents\foo.txt");
This is the equivalent of the VBA:
Set ole = sh.OLEObjects.Add(Filename:="C:\Users\hambone\Documents\foo.txt")
The method returns an OLEObject object, which you can then shape to behave the way you want:
ole.Height = 5;

C# Interop Excel Range get_End is returning with 1 less element

I need to parse an Excel file. First I wrote an extension in Visual Basic inside the Excel file, all worked good. Now I need to port it to C# so it can be a separate application. While the functions I use are the same, the result is not the same...
When I choose from the GUI which Worksheet to parse, I do something like:
range = (workbook.Worksheets.get_Item(itemIndex) as Excel.Worksheet).UsedRange;
Then, for the first row I need to parse I do something like:
range.get_Range(range.Cells.get_Item(6, 2),
range.Cells.get_Offset(6,2).get_End(Excel.XlDirection.xlToRight)))
And I get the right result with all the fields I need.
The second time when I need to get another row, I do:
range.get_Range(range.Cells.get_Item(13, 3),
range.Cells.get_Offset(13, 3).get_End(Excel.XlDirection.xlToRight)))
This time it gives me all the elements except the last one. And I have more functions like this, some with XlDirection.xlDown and all of them return me the range without the last element.
I tried to swap the functions, thinking may be I need to release range and then acquire it again or something(wanted to check if it's always working only for the first function being executed) but it is always working only for the first example, whenever the function is being executed...
This is even stranger because it worked in VBA Excel.
I also tired with Excel.Application get_Range and Excel.Worksheet get_Range...
Anyone knows why this happens?
I managed to solve this strange behavior. It's not the correct way of getting out the data.
The correct way would be: range.get_Range(range.Cells[6, 2], (range.Cells[6, 2] as Excel.Range).get_End(Excel.XlDirection.xlToRight)) - for the first example.
Hope it helps somebody...

How to append Values to Excel Range

I have an Excel workbook which has 3 worksheets with a named range defined for address A1:F10 in each sheet and have some values.
I wanted to have a range defined and set all the 3 worksheet range values to this.
Example
TempRange = Test1Range + Test2Range + Test3Range
Any help would be appreciated
IF all four ranges are the same size, then you can add 'em up with an array formula. Select the entire area of "TempRange", and type
=Test1Range + Test2Range + Test3Range
... and then, to enter it, hit CTRL+SHIFT+ENTER, not plain ENTER.
It's been a long time, and I use OpenOffice these days, so I might have details wrong. The general idea works, though, so even if this exact thing isn't right, googling for "Excel array formula" will probably turn up some helpful information.
I found the answer
Application.Union(Range1,Range2)

C# Clipboard Help

I am copying the contents of an Excel file onto the Clipboard within a program I have written. I can then use that data in memory rather than 'chatting' constantly with Excel.
When I have finished with the data, I cal a cleanup method that calls Clipboard.Clear() first and then closes all Excel sheets/workbooks/apps, etc.
The problem is, even though I clear the Clipboard prior to closing the Excel sheets, I get a pop up window still saying there is substantial amount of data on the clipboard. Anybody know why?
Thanks,
Darren.
Not sure why that happens, but have you tried setting _Application.DisplayAlerts = false; (MSDN) before you close the sheets to see if that prevents the warning message?
You could try setting the Excel CutCopyMode property to cancel the current copy information:
Application.CutCopyMode = false;
Another thought is to set the clipboard to String.Empty so the amount of data copied is small enough that it bypasses the warning popup. This may have to be done from the Excel sheet, not the regular clipboard (i.e., copy a cell from the active sheet in Excel).
Perhaps you could use
Application.CutCopyMode = false;
http://msdn.microsoft.com/en-us/library/ff839532.aspx
Well at the end, try copying an empty string in clipboard and leave it, then Excel may not give any warning. But use Excel API to copy empty string in clipboard.

Categories

Resources