我得到了一個功能,我需要保護在服務器上傳的Excel文件,它不能被編輯或保存。 爲了達到這個目的,我以編程方式保護文件並添加了一些VBA代碼,限制了用戶可以使用它進行的操作。 VBA代碼函數假設有2個功能,它可以隱藏功能區並阻止保存/另存爲函數。 你可以參考我的代碼裏面有3個版本的VBA代碼,但最希望的是其中的3號VBA代碼。 這是我爲此目的的函數,我將字符串中的文件位置傳遞給此函數。 我希望有人可以幫助我如何讓程序保存安全的Excel文件(使用VBA代碼no.3)。 從我的理解,我無法保存它由於VBA代碼保存(不SaveAs)部分將不會允許此行wbkExcel.Save()過程。使用VBA通過VBA在Excel中禁用保存功能#
- 你可以在你的Excel文件來測試VBA代碼看看它的工作或沒有(測試工作對我來說,到目前爲止...)
在此先感謝大家......
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();
}
我不確定這會起作用。用戶不能按住shift鍵來禁用自動執行宏?在更新版本的Excel中,代碼不會自動運行,除非它位於受信任的位置,所以我無法想象這是防彈的。 – lfrandom
是的,你是對的,這些文件將被保存爲.xlsm格式,我想要求用戶更新他們的註冊表,我打算允許宏運行,但如果我這樣做會成爲安全問題。不是100%防彈,但比沒有更好,我會繼續尋找另一種解決方案,如果有任何提高它。添加此VBA代碼後,該文件將受密碼保護(工作簿和工作表)。如果您有任何建議,請告訴我。 – mutanic