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

时间:2015-10-26 14:16:10

标签: excel passwords password-protection excel-2013 multiple-files

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

我之前手动密码保护每个单独的.xlsx文件但是想知道是否有自动方式我可以这样做?这是因为我定期更新这些文件(为了方便删除密码),然后在发送之前重新申请密码。

1 个答案:

答案 0 :(得分:1)

以下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

由于打开和关闭文件需要时间,因此这不是一个非常快速的过程。以下例程实际上更快,但它们 心理上更快 ,正如您在状态栏中可以看到正在处理的文件。

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