Monday 16 June 2014

Protect excel with password

Here is the VBA Code to protect an excel sheet with the password

Please make sure that the libraries have interop dll .
Microsoft.Office.Interop.Excel.dll

Module Module1

    Sub Main()


     
        Dim strfilename As String = "c:\mssql\DW_Reporting\Templates\dwreport.xls"
        Dim strnewfilename As String = "c:\MSSQL\SSIS\dwReport\protected\DWDailyReport.xls"

        Dim strPassword As String = "pwd"


        Dim objExcel As New Microsoft.Office.Interop.Excel.Application

        Dim objWorkbook As Microsoft.Office.Interop.Excel.Workbook

        objWorkbook = objExcel.Workbooks.Open(strFilename)

        objExcel.DisplayAlerts = False

        objWorkbook.SaveAs(strnewfilename, Password:=strPassword)

        objWorkbook.Close()

   
    End Sub

No comments:

Post a Comment