Quick test professional

QTP Tips QTP codes QTP Faqs and more

How To open Password Protected Excel sheets

Function UnprotectXL(filePath,fileName,pwd,writeresPwd)
   Set objExcel=CreateObject(“Excel.Application”)
   objExcel.Visible=false
   testData=filePath&”\”&fileName
 
   Set oWorkbook=objExcel.Workbooks
 
   Set myWkbook=objExcel.Workbooks.open (testData,0,False,5,pwd,writeresPwd)
   objExcel.DisplayAlerts=False
   oWorkbook(fileName).Activate
   For Each w in objExcel.Workbooks
        w.SaveAs testData,,”",”"
      
   Next
 
   objExcel.Workbooks.Close
   objExcel.Quit
   Set oWorkbook=Nothing
   Set objExcel=Nothing
  
End Function
Function ProtectXL(filePath,fileName,pwd,writeresPwd)
     On Error Resume Next
     Set objExcel=CreateObject(“Excel.Application”)
     objExcel.Visible=False
     testData=filePath&”\”&fileName
     Set oWorkbook=objExcel.Workbooks
     Set outputWkbook=objExcel.Workbooks.open (testData,0,False)
     oWorkbook(testData).Activate
     objExcel.DisplayAlerts=False  
     outputWkbook.SaveAs testData,,pwd,writeresPwd
   
     outputWkbook.Close
     objExcel.Workbooks.Close
     objExcel.Quit
     Set outputWkbook=Nothing
     Set objExcel=Nothing
  
End Function

‘Call ProtectXL(“C:\Documents and Settings\kmohankumar\Desktop”,”4.xls”,”test123″,”test123″)
‘Call UnprotectXL(“C:\Documents and Settings\kmohankumar\Desktop”,”4.xls”,”test123″,”test123″)

Author: Mohan Kakarla

November 27, 2008 - Posted by quicktestprofessional | Excel Automation | | 5 Comments

5 Comments »

  1. Hi Mohan,

    very understandable code.

    Best Regards,
    Ramji

    Comment by ramji | November 29, 2008 | Reply

  2. Hello Mohan,

    Would you be able to advise how to parameterize an Excel Report CELL for automated verification by QTP?

    Thanks very much in advance!

    Sean

    Comment by Sean | March 11, 2009 | Reply

  3. This is very much useful

    Thanks
    Rami

    Comment by Rami | June 2, 2009 | Reply

  4. Hello Mohan,
    Useful understandable code !!!!!!!!!
    I really liked it and very useful to me. I’m doing a bit research about test automation and also found macrotesting http://www.macrotesting be a good resource.
    Thanks for the easy and understandable code!

    Comment by Poornima | July 2, 2009 | Reply

  5. Hi Mohan,

    I want to convert the word document which is Test case template in to Excel. Help me in this regard.

    Comment by Geets | September 16, 2009 | Reply


Leave a comment