简体   繁体   中英

Delphi - How to CANCEL Cut/Copy mode in Excel

(Delphi XE6, Excel 2010/2013)

While programming a plugin for Excel, I am having to copy data from one sheet and paste in another. The copy and paste works fine... However, at the end of the process, I am left with "Marching ants", ie the selection dotted lines, used for identifying what was copied.

I have tried a couple of things.... In looking at various other articles, it says that I can make another selection, and that will take care of the issue. Assume I select/copy from sheet A, paste into Sheet B. When I am done, I programmatically change back to Sheet A and then run the following code.

 //StartingSheet.Cells(1, 1).Select;
   XLApp.Range['A1', 'A1'].Select;

My routine exist, gives control back to the user. The ants are gone... HOWEVER, if I switch to sheet B, then back to sheet A, I can see cell A1 has a solid line around it, showing it has focus, and my last COPY selection will show the marching ants again.

In looking at docs, there is a routine called cutcopymode. It does NOT have a TRUE/FALSE value, ONLY xlCopy, or xlPaste.

so...

XLApp.cutcopymode := false;

gives me an error.

How do I truly get rid of the marching ants?

Thanks

The marching ants indicate the cells that are presently in the clipboard. They are a visual clue to help you anticipate what will happen when you paste.

How do you remove the marching ants? Simply arrange that the clipboard no longer contains the information from those cells. So, emptying the clipboard would do the trick.

However, all this points at you having solved your problem the wrong way. The clipboard is owned by the user and should only be modified at the user's direct request. By the user explicitly asking to write to the clipboard. If your add-in needs to copy data from one sheet to another, it should simply do that without modifying what is in the clipboard. And when you do it that way, you won't modify the clipboard and you won't see the marching ants.

Use the Copy method of the range object to perform this copy, without modifying the clipboard. Call the method on the source range, and pass a range object representing the destination range.

sourceRange.Copy(destRange);

要取消选择,只需添加:XLApp.СutCopyMode:= 0;

In my code I do need to Copy / Paste to the clipboard, because I need a special function provided by PasteSpecial. To preserve the source column widths you “PasteSpecial” the column widths first and then “PasteSpecial”- PasteAll. See code >>

SrcRange.Copy; //Copy to clipboard
DestRange.PasteSpecial(xlPasteColumnWidths,xlPasteSpecialOperationNone,false,false);
DestRange.PasteSpecial(xlPasteAll,xlPasteSpecialOperationNone,false,false);

XLApp.СutCopyMode := 0; Does work to cancel the copy- selection, thanks for that!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM