我开发了excel模板并遇到了以下问题...
在某些情况下我必须验证单元格。必须保留单元格的格式。如果用户将不同工作表中的值复制到模板,则单元格格式将被覆盖。有没有办法在excel中捕获粘贴事件,并使用C#
使用pastespecial提前致谢....
答案 0 :(得分:2)
我用Google搜索了2到3天并找到了解决方法,然后使用我们必须检查信任访问vba对象模型的宏设置....来运行项目的excel ...
//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");