trap paste event in excel and allow only pastespecial
I developed the excel template and come across the following problem...
There are scenarios where i have to validate the cell.Hence format of the cell must be preserved.If user copies value from different sheet to the template the format of cell get overwritten. Is开发者_开发问答 there a way to trap the paste event in excel and use pastespecial using C#
Thanks in advance....
I googled out for 2 to 3 days and find the work around,before using the code we have to check the trust acess to vba object model in macro setting....of the excel to run the project..
//Add macro module
Microsoft.Vbe.Interop.VBComponent component = this.VBProject.VBComponents
.Add(Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_StdModule);
component.CodeModule.AddFromString("Sub Paste_cell()" + Environment.NewLine + "If MsgBox(\"Normal paste operation has been disabled. You are about to Paste Values (cannot be undone), proceed?\", vbQuestion + vbOKCancel, GSAPPNAME) = vbOK Then" + Environment.NewLine + "On Error Resume Next" + Environment.NewLine + "ActiveSheet.PasteSpecial Format:=\"Text\", Link:=False, DisplayAsIcon:=False" + Environment.NewLine + "Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False" + Environment.NewLine + "End If" + Environment.NewLine + "End Sub");
//trap paste event
CommonData.DATASHEET.Application.OnKey("^v", "Paste_cell");
精彩评论