好库网 好库网首页 | 我的好库
VB爱好者

VB如何打开或保存为有密码保护的Excel sheets工作表

发布者:vbfans
发布日期:2011/5/15 22:31:31   更新日期:2011/5/15 22:31:31
阅读次数:4737
评分:4.80
介绍:VB如何利用automation(自动化)打开有密码保护的Excel sheets工作表,或者保存为有密码保护的Excel文件。
正文:

对于有密码保护的excel文件,如何读取呢?

请看代码:

 

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

有时候为了保护EXCEL,我们需要加上密码,请看代码:

 

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\okbase\Desktop","4.xls","test123","test123")
Call UnprotectXL("C:\Documents and Settings\okbase\Desktop","4.xls","test123","test123")

文件名为4.xls,密码为test123

 

 

 

 


评论 [发表评论]
账号 密码 还没帐号呢,现在注册一个?

免责声明:好库网所展示的信息由买卖双方自行提供,其真实性、准确性和合法性由信息发布人负责。好库网不提供任何保证,并不承担任何法律责任。