密码保护目录中的多个文件

我有一个包含50个.xlsx文件的目录。 我需要将这些发送给某人,由于他们的工作环境限制,我无法使用Winzip。

我以前的密码手动保护每个单独的.xlsx文件,但想知道是否有一个自动化的方式,我可以做到这一点? 这是因为我正在对这些文件进行定期更新(轻松删除密码),然后在发送之前重新应用密码。

下面的VBA例程将打开所有的文件(你不会看到它),并将保存它们或者用密码或不用。

Option Explicit Const FOLDER As String = "C:\Temp\Test xl file bulk pw protection\" Const PASSWORD As String = "weakpassword" Dim app As Excel.Application Dim strFile As String Dim wb As Workbook Sub Password_ON() Set app = New Excel.Application strFile = Dir(FOLDER) app.DisplayAlerts = False Do While Len(strFile) > 0 Set wb = app.Workbooks.Open(FOLDER & strFile) wb.SaveAs wb.FullName, , PASSWORD wb.Close strFile = Dir Loop app.DisplayAlerts = True app.Quit Set app = Nothing End Sub Sub Password_OFF() Set app = New Excel.Application strFile = Dir(FOLDER) app.DisplayAlerts = False Do While Len(strFile) > 0 Set wb = app.Workbooks.Open(FOLDER & strFile, , , , PASSWORD) wb.SaveAs wb.FullName, , vbNullString wb.Close strFile = Dir Loop app.DisplayAlerts = True app.Quit Set app = Nothing End Sub 

由于打开和closures文件需要时间,所以这不是一个快速的过程。 下面的例程实际上并不快,但它们在心理上更快 ,正如你可以在状态栏中看到哪个文件正在被处理。

 Sub Password_ON() Set app = New Excel.Application strFile = Dir(FOLDER) app.DisplayAlerts = False Do While Len(strFile) > 0 Application.StatusBar = "Processing " & strFile DoEvents Set wb = app.Workbooks.Open(FOLDER & strFile) wb.SaveAs wb.FullName, , PASSWORD wb.Close strFile = Dir Loop app.DisplayAlerts = True app.Quit Set app = Nothing Application.StatusBar = "READY" End Sub Sub Password_OFF() Set app = New Excel.Application strFile = Dir(FOLDER) app.DisplayAlerts = False Do While Len(strFile) > 0 Application.StatusBar = "Processing " & strFile DoEvents Set wb = app.Workbooks.Open(FOLDER & strFile, , , , PASSWORD) wb.SaveAs wb.FullName, , vbNullString wb.Close strFile = Dir Loop app.DisplayAlerts = True app.Quit Set app = Nothing Application.StatusBar = "READY" End Sub