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.
Related
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 know this is asked many times before but it s not what i look for, to make copy paste in c# we use;
//Copy
Clipboard.SetDataObject("String to copy");
//Paste
IDataObject iData = Clipboard.GetDataObject();
I only want to use copy, after using:
Clipboard.SetDataObject("String to copy");
when i close the program, then when i right click and paste on a txt file nothing happens!
So Clipboard class seems doesnt help, so i need another solution.
Clipboard.SetDataObject("String to copy", true);
That boolean value at the end specifies whether the string should remain in the clipboard after the application closes, and is set to false as default.
You want to use second overload of SetDataObject(object, bool).
When bool is set to true, data will remain after application is closed.
Did you try to use Clipboard.SetText(your_String)?
This works for me.
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()
I'm doing this in C#, but, I guess, it isn't language specific problem...
I've got some sample code on how to detect when the contents of the clipboard changes. Now I want to modify the text that just got copied (strip some tags) and replace clipboard text with fixed one.
But if I SetDataObject() when I detect the clipboard contents have changed, that will generate the the message that Clipboard contents have changed again. In theory, i guess, it sounds like an infinite loop (in practice for some reason it's not).
What's the strategy to do this modification once, and sent the message down the clipboard monitoring chain?
P.S. As a test I'm doing following, but what happens is Clipboard.SetDataObject.. line gets called twice. I don't understand why, I'd expect it do this infinitely.
case Win32.Msgs.WM_DRAWCLIPBOARD:
String clipboardText = GetClipboardText();
if (!String.IsNullOrEmpty(clipboardText))
{
Clipboard.SetDataObject("test( " + clipboardText + " )!");
}
Win32.User32.SendMessage(_ClipboardViewerNext, m.Msg, m.WParam, m.LParam);
Unfortunately, any time you set the Clipboard's data with Clipboard.SetDataObject, you'll "redraw" the clipboard, since you're changing its contents.
The best way to handle this situation is to make your routine check the clipboard contents, and only set it if you actually change the clipboard text.
In your case, you say you're stripping out some tags. Just check the clipboardText string before and after your routine, and if the string is unchanged, don't set it again.
This way, the first time, it'll strip the tags out, then reset. Then fire again, see there are no changes, and not reset the clipboard data.
To prevent the recursion, you should only actually set the clipboard text if there were tags to strip.
To explain the behavior, I would guess that WM_DRAWCLIPBOARD isn't sent recursively, meaning that if the clipboard is changed during a WM_DRAWCLIPBOARD notification, it won't be sent again.
Actually there is a much better way to avoid infinite loop which works even in cases when you are not modifying clipboard content and consequently you cannot compare if you changed it or not.
When you detect WM_DRAWCLIPBOARD message call GetClipboardOwner which will return handle to the window which modified the handle. Once you have the handle you can get the process which owns the window. If the process is different from your process then process the clipboard, otherwise the clipboard was changed by you so ignore it.