2014-05-14 82 views
2

我得到了一個功能,我需要保護在服務器上傳的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(); 
} 

回答

0

經過很多嘗試&代碼錯誤,我終於設法實現我所需要的,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"; 

我從我以前的代碼注意什麼,指揮(VBA代碼)取消=真私人小組Workbook_BeforeSave將阻止命令(C#)wbkExcel.Save()不能運行,因爲VBA代碼將在保存過程中返回C#代碼的false。如果我在這裏錯了,我希望有人能幫助並糾正我。希望這個人也會幫助其他人。感謝StackOverflow.com。

+0

我不確定這會起作用。用戶不能按住shift鍵來禁用自動執行宏?在更新版本的Excel中,代碼不會自動運行,除非它位於受信任的位置,所以我無法想象這是防彈的。 – lfrandom

+0

是的,你是對的,這些文件將被保存爲.xlsm格式,我想要求用戶更新他們的註冊表,我打算允許宏運行,但如果我這樣做會成爲安全問題。不是100%防彈,但比沒有更好,我會繼續尋找另一種解決方案,如果有任何提高它。添加此VBA代碼後,該文件將受密碼保護(工作簿和工作表)。如果您有任何建議,請告訴我。 – mutanic