使用VBA通过C#禁用在Excel中保存function

我有一个function,我需要保护Excelfile upload到服务器,无法编辑,甚至保存。 为了实现这一点,我以编程方式保护文件,并添加了一些VBA代码限制了用户可以使用它。 VBA代码函数假设有2个function,可以隐藏function区并阻止Save / SaveAsfunction。 你可以参考我的代码里面有3个版本的VBA代码,但最希望的是其中的3号VBA代码。 这是我的function,我将文件位置的string传递给这个function。 我希望有人可以帮助我如何让程序保存安全的Excel文件(使用VBA代码no.3)。 从我的理解,我不能保存由于VBA代码保存 (不保存 )部分将不会允许这行wbkExcel.Save()进行。

  • 您可以在Excel文件中testingVBA代码,看看它是否正常工作(到目前为止,testing对我有用…)

在此先感谢大家…

protected void ExcelEncryptor(string strExcelFile) { Microsoft.Office.Interop.Excel.Application wAppExcel = new Microsoft.Office.Interop.Excel.Application(); wAppExcel.Interactive = false; wAppExcel.Visible = true; wAppExcel.DisplayAlerts = false; wAppExcel.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable; Microsoft.Office.Interop.Excel.Workbook wbkExcel = wAppExcel.Workbooks.Open(strExcelFile.ToString(),System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value); string strVBCode = string.Empty; //To hide Ribbon only /*strVBCode = "Private Sub Workbook_Open()\r\n" + " msgbox \"This document is protected!\"\r\n" + " application.ExecuteExcel4Macro \"show.toolbar(\"\"Ribbon\"\",False)\"\r\n" + "End Sub";*/ //To hide Ribbon + Disable SaveAs (F12 key) but still can Save (Ctrl+S key) /*strVBCode = "Private Sub Workbook_Open()\r\n" + " msgbox \"This document is protected!\"\r\n" + " application.ExecuteExcel4Macro \"show.toolbar(\"\"Ribbon\"\",False)\"\r\n" + "End Sub\r\n" + "Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)\r\n" + " If (SaveAsUI = True) Then\r\n" + " msgbox \"You are not allowed to save this document!\"\r\n" + " Cancel = True\r\n" + " End If\r\n" + "End Sub";*/ //To hide Ribbon + Disable SaveAs (F12 key) and cannot Save (Ctrl+S key) strVBCode = "Option Explicit\r\n" + "Dim SaveByCode As Boolean\r\n" + "Const msg As String = \"You are not allowed to save this document!\"\r\n" + "Const ttl As String = \"This document is protected!\"\r\n" + "Private Sub Workbook_Open()\r\n" + " MsgBox msg, vbExclamation, ttl\r\n" + " application.ExecuteExcel4Macro \"show.toolbar(\"\"Ribbon\"\",False)\"\r\n" + "End Sub\r\n" + "Private Sub Workook_BeforeClose(Cancel As Boolean)\r\n" + " If Me.Saved = False And SaveByCode = False Then\r\n" + " MsgBox msg, vbExclamation, ttl\r\n" + " Cancel = True\r\n" + " End If\r\n" + "End Sub\r\n" + "Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)\r\n" + " Application.EnableEvents = False\r\n" + " If SaveByCode = True Then\r\n" + " SaveThisFile\r\n" + " Else\r\n" + " MsgBox msg, vbExclamation, ttl\r\n" + " Cancel = True\r\n" + " End If\r\n" + " Application.EnableEvents = True\r\n" + "End Sub\r\n" + "Sub SaveThisFile()\r\n" + " SaveByCode = True\r\n" + " ThisWorkbook.Save\r\n" + "End Sub"; Microsoft.Vbe.Interop.VBProject vbMacro = wbkExcel.VBProject; Microsoft.Vbe.Interop.VBComponent vbCode = vbMacro.VBComponents.Item("ThisWorkBook"); Microsoft.Vbe.Interop.CodeModule vbModule = vbCode.CodeModule; vbModule.AddFromString(strVBCode.ToString()); wbkExcel.Protect("Pa$$w0rd!", true, false); foreach (Microsoft.Office.Interop.Excel.Worksheet wstExcel in wAppExcel.Worksheets) { wstExcel.Protect("Pa$$w0rd!", true, true, true, true, false, false, false, false, false, false, false, false, true, true, false); } wbkExcel.Save(); wbkExcel.Close(System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value); Marshal.ReleaseComObject(wbkExcel); Marshal.ReleaseComObject(wAppExcel); GC.Collect(); } 

经过大量的代码尝试和错误,我终于设法达到我所需要的,Excel文件将被保护+不能按F12键+不能按Ctrl + S键+不能再按Ctrl + P键,更新后的VBA代码也可以防止用户打开Visual Basic编辑器,如果他们想禁用代码,但在我的情况下,我现在就可以访问它。 请参考下面我更新的VBA代码:

  //Excel file will be password protected+cannot press F12 key+cannot press Ctrl+S key+cannot press Ctrl+P key anymore strVBCode = "Private Sub Workbook_Open()\r\n" + " msgbox \"This document is protected!\"\r\n" + " application.ExecuteExcel4Macro \"show.toolbar(\"\"Ribbon\"\",false)\"\r\n" + " ThisWorkbook.Saved = True\r\n" + " Application.OnKey \"^s\", \"\"\r\n" + " Application.OnKey \"^p\", \"\"\r\n" + //Enable this line if you wish to block Visual Basic Editor //" Application.OnKey \"%{F11}\", \"\"\r\n" + "End Sub\r\n" + "Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)\r\n" + " If (SaveAsUI = true) Then\r\n" + " msgbox \"You are not allowed to save this document!\"\r\n" + " Cancel = True\r\n" + " Else\r\n" + " ThisWorkbook.Saved = True\r\n" + " Application.OnKey \"^s\", \"\"\r\n" + " Application.OnKey \"^p\", \"\"\r\n" + //Enable this line if you wish to block Visual Basic Editor //" Application.OnKey \"%{F11}\", \"\"\r\n" + " End If\r\n" + "End Sub"; 

我从前面的代码中注意到,在Private Sub Workbook_BeforeSave中 取消= True的命令(VBA代码) True将阻止命令(C#) wbkExcel.Save()运行,因为VBA代码将在保存过程中为C#代码返回false。 我希望有人能帮助和纠正我,如果我在这里错了。 希望这个也能帮助别人。 感谢StackOverflow.com。