Save as a Password Protected Excel sheet

Share this post
FaceBook  Twitter  Mixx.mn     

To Open existing excel sheet and save a password protected excel spreadsheet, use following code:

1)  Create a Excel object

2) Set Visible property as True

3) Open workbook and activate

4) Save workbook with a password.

5) Close workbook and Excel

6) Discard all variables

 

'1)  Create a Excel object
     Set objExcel=CreateObject("Excel.Application")

'2) Set Visible property as True
     objExcel.Visible=True
     fileName ="C:\......\abc.xls"
     excelpwd="pass"

'3) Open workbook and activate
     Set oWorkbook=objExcel.Workbooks
     Set outputWkbook=objExcel.Workbooks.open (fileName,0,False)
     oWorkbook("sheet1").Activate
     objExcel.DisplayAlerts=False 

'4) Save workbook with a password.
     outputWkbook.SaveAs fileName,,excelpwd,excelpwd

'5) Close workbook and Excel  
     outputWkbook.Close
     objExcel.Workbooks.Close
     objExcel.Quit

'6) Discard all variables

     Set outputWkbook=Nothing
     Set objExcel=Nothing