I am working on C# VSTO(excel). I have created excel workbook project.
I have been trying to implement Paste Special for my workbook.
Manually, we do paste special like This
But i need C# code to do the same.
Is there any way so i can apply it for every copy/cut paste operation in my excel workbook?
I don't want to use VBA Macros as it asks everytime user to whether he wants to ENABLE MACROS OR NOT and hence is there another way o accomplish this.
Have you tried PasteSpecialmethod over ranges, it provides numerous options to copy formats/ column widths/ fomats with values etc.
Also there is another copy special to copy/paste as picture.
to copy Range:
Range.CopyPicture(xlPrinter, xlPicture);
// the range gets copied in clipboard
// there are options available like xlScreen in case you want to copy as it appears on screen.
Sheet.Paste()
// this will paste the shape in the sheet, to paste in some range you could use range.Paste()
Related
I have the requirement to create an Bitmap Object from a Excel Path inside a ms-word document, and I can't seem to find a way to do it in one step.
The current solution is the following.
Create a Field Object
Unlink the Field Object
Like this:
Dim fld As Field
Set fld = Selection.Fields.Add(Selection.Range, WdFieldType.wdFieldLink, "Excel.Sheet.12 ""[Path to Xlsx]"" ""[Sheet+Range]"" \b", False)
fld.Update
fld.Unlink
\b says the field should represented as a Bitmap object.
With fld.Unlink I am dropping the link and the Bitmap stays.
Is there a way to directly create the Bitmap Object (InlineShape) without the field approach?
I tried to record a macro in ms-word to see how ms-word is doing it but saddly it only gives:
Selection.PasteSpecial Link:=False, DataType:=wdPasteBitmap, Placement:= wdInLine, DisplayAsIcon:=False
If possible I would like to avoid working with Clipboard and PasteSpecial.
Edit:
Since the given answer has shown me that my question wasn't as clear as I hoped it to be.
So first of all, the only Information I currently have are
The path to an XLSX file
The Sheet and Range of said XLSX File
eg.
C:\test\myWorkbook.xlsx
Sheet1!A1:D10
Thats all the Information I have.
With this Information I need to programatically create a Bitmap inside my ms-word document.
As shown above I already have a solution which does exactly this, by doing a LINK to the said XLSX file and after it got updated dropping that link to only have the Bitmap left.
A Bitmap is staying here because of the \b flag which tells the LINK to produce a Bitmap, instead of a RTF/HTML.
My question now is, if there is another way I am currently doing it.
The current answer, does indeed insert a Bitmap into my ms-word document, but where do I get the Bitmap from, as the answer isn't aware of the fact that I need an image of an excel workbook? As I said earlier, I only have limited information and no Bitmap.
What I want to achieve can be done without programatically by using the Paste Special functionality
https://support.office.com/en-us/article/paste-special-e03db6c7-8295-4529-957d-16ac8a778719
Open Xlsx/Docx
Copy Cells from Xlsx
Go to Docx
Use Paste Special
Select Bitmap
Hit OK
The output you receive in your Docx is what I desire to make programatically, without Clipboard and Paste Special and with a, if possible, better solution than I currently have.
You can use the following code:
string fileName = "c://msdn.bmp"; //the picture file to be inserted
Object oMissed = doc.Paragraphs[2].Range; //the position you want to insert
Object oLinkToFile = false; //default
Object oSaveWithDocument = true;//default
doc.InlineShapes.AddPicture(fieldName, ref oLinkToFile, ref oSaveWithDocument, ref oMissed);
See insert a picture into a word document using c#? for more information.
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;
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);
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...
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.