vba - EXCEL VBA - 将工作簿导出到受密码保护的 ZIP 文件

标签 vba excel

是否有办法修改 Ron De Bruin 的代码以将当前工作簿导出到密码保护 zip 文件。我已经环顾了一段时间,但不知道如何启用这样的选项。

代码在这里:http://www.rondebruin.nl/win/s7/win001.htm

Sub NewZip(sPath)
'Create empty Zip File
'Changed by keepITcool Dec-12-2005
    If Len(Dir(sPath)) > 0 Then Kill sPath
    Open sPath For Output As #1
    Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
    Close #1
End Sub

Sub Zip_ActiveWorkbook()


 Dim strDate As String, DefPath As String
    Dim FileNameZip, FileNameXls
    Dim oApp As Object
    Dim FileExtStr As String

    DefPath = "C:\Users\Ron\test\"    '<< Change
    If Right(DefPath, 1) <> "\" Then
        DefPath = DefPath & "\"
    End If

    'Create date/time string and the temporary xl* and Zip file name
    If Val(Application.Version) < 12 Then
        FileExtStr = ".xls"
    Else
        Select Case ActiveWorkbook.FileFormat
        Case 51: FileExtStr = ".xlsx"
        Case 52: FileExtStr = ".xlsm"
        Case 56: FileExtStr = ".xls"
        Case 50: FileExtStr = ".xlsb"
        Case Else: FileExtStr = "notknown"
        End Select
        If FileExtStr = "notknown" Then
            MsgBox "Sorry unknown file format"
            Exit Sub
        End If
    End If

    strDate = Format(Now, " yyyy-mm-dd h-mm-ss")

    FileNameZip = DefPath & Left(ActiveWorkbook.Name, _
    Len(ActiveWorkbook.Name) - Len(FileExtStr)) & strDate & ".zip"

    FileNameXls = DefPath & Left(ActiveWorkbook.Name, _
    Len(ActiveWorkbook.Name) - Len(FileExtStr)) & strDate & FileExtStr

    If Dir(FileNameZip) = "" And Dir(FileNameXls) = "" Then

        'Make copy of the activeworkbook
        ActiveWorkbook.SaveCopyAs FileNameXls

        'Create empty Zip File
        NewZip (FileNameZip)

        'Copy the file in the compressed folder
        Set oApp = CreateObject("Shell.Application")
        oApp.Namespace(FileNameZip).CopyHere FileNameXls

        'Keep script waiting until Compressing is done
        On Error Resume Next
        Do Until oApp.Namespace(FileNameZip).items.Count = 1
            Application.Wait (Now + TimeValue("0:00:01"))
        Loop
        On Error GoTo 0
        'Delete the temporary xls file
        Kill FileNameXls

        MsgBox "Your Backup is saved here: " & FileNameZip

    Else
        MsgBox "FileNameZip or/and FileNameXls exist"

    End If
End Sub

最佳答案

我在另一个使用 7Zip 的网站上找到了一个可接受的答案......

strDestFileName = "c:\temp\TestZipFile.zip"   
strSourceFileName = "c:\temp\test.pdf"   
str7ZipPath = "C:\Program Files\7-Zip\7z.exe"   
strPassword = "MyPassword"   

strCommand = str7ZipPath & " -p" & strPassword & " a -tzip """ & strDestFileName & """ """ & strSourceFileName & """"
Shell strCommand 

关于vba - EXCEL VBA - 将工作簿导出到受密码保护的 ZIP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22448766/

相关文章:

Excel VLOOKUP 3 列

excel - 每 15 分钟运行一次宏

vba - Excel自定义格式添加“之前和之后的记录

java - 文件大小问题。当 poi 读写时

excel - 定义具有两组值的变量

excel - 当我想要部分匹配时使用 =IF(OR 公式

excel - 如何替换字符串中的第一个字符

VBA - 在公式中使用 Columns()

excel - BizTalk BAM Excel "Unable to get resource string"

excel - 如何检查是否关闭excel(工作簿)vba