VBA 把文件夹压缩成ZIP文件

Option Explicit

Sub sbCreateEmptyZIP(prmStrFileNameZip)

    'If the file already exists then delete
    If Len(Dir(prmStrFileNameZip)) > 0 Then Kill prmStrFileNameZip
    
    'Create empty ZIP File
    Open prmStrFileNameZip For Output As #1
    Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
    Close #1
End Sub

Sub sbZIP()
    Dim strFullPath As String
    Dim strParentPath As String
    Dim strFolderName As String
    Dim strFileNameZip As Variant
    Dim objApp As Object

    'Select the folder you want to compress
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Please select a folder you want to compress."
        If .Show = True Then strFullPath = .SelectedItems(1)
    End With
    If strFullPath = "" Then Exit Sub

    'Get the parent path of the selected folder
    strParentPath = Left(strFullPath, InStrRev(strFullPath, "\"))
    'Get the folder name
    strFolderName = Mid(strFullPath, InStrRev(strFullPath, "\") + 1)

    'Set Zip file name
    strFileNameZip = strParentPath & strFolderName & Format(Now, "yyyymmddhmmss") & ".zip"
    
    'Create a new empty Zip
    Call sbCreateEmptyZIP(strFileNameZip)
    
    'Copy the files to the compressed folder
    Set objApp = CreateObject("Shell.Application")
    objApp.Namespace(strFileNameZip).CopyHere objApp.Namespace(strFullPath)
    On Error Resume Next

    MsgBox "The ZIP File has been created." & vbCrLf & strFileNameZip

End Sub

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VBA解压缩ZIP文件可以使用WinRAR的命令进行调用。在VBA中,可以参考WinRAR的帮助文档来了解解压缩文件的命令和示例。例如,在Windows资源管理器中右键单击ZIP文件夹并选择全部解压缩,这将解压缩路径为Path的ZIP文件夹。如果解压缩成功,返回结果为0,否则返回错误代码。 解压缩ZIP文件的过程通常使用循环来控制。首先,我们需要获取ZIP文件的基本名称(BaseName),然后根据现有文件的保留规则修改ZIP文件的名称。例如,将"Example.zip"修改为"Example (2).zip"。然后根据循环条件,逐次增加版本号,构建新的ZIP文件路径。当文件不存在或版本号超过最大版本限制时,循环结束。如果版本号超过最大限制,则表示无法创建ZIP文件,抛出相应的错误。 以上是使用VBA解压缩ZIP文件的一种方法。您可以根据具体需要调用合适的命令和方法进行操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [VBA解压压缩文件。](https://blog.csdn.net/qq_24499417/article/details/81878685)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [vba 压缩文件_使用Windows资源管理器方式通过VBA压缩和解压缩文件文件夹](https://blog.csdn.net/cunchi8090/article/details/107521161)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值