首页 > 解决方案 > 如何让 Excel VBA 在继续之前等待解压缩所有文件

问题描述

我正在尝试从一个 .zip 文件中解压缩 589 个 PDF 文件,但在检查目标文件夹中的解压缩文件后,只有 100 多个文件被解压缩。我猜代码不会等待。

我想我从 Ron de Bruin 那里得到了这段代码,但我找不到关于如何让我的脚本等待所有文件被提取后再继续下一步的代码。

Sub UnzipFile_Click()
Dim FSO As Object
Dim oApp As Object
Dim Fname As Variant
Dim FileNameFolder As Variant
Dim DefPath As String
Dim strDate As String

Application.ScreenUpdating = False


Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", _
                                        MultiSelect:=False)
If Fname = False Then
    'Do nothing
Else
    'Root folder for the new folder.
    'You can also use DefPath = "C:\Users\Ron\test\"
    DefPath = Application.DefaultFilePath
    If Right(DefPath, 1) <> "\" Then
        DefPath = DefPath & "\"
    End If

    'Create the folder name
    strDate = Format(Now, " mm-dd-yy")
    FileNameFolder = DefPath & "DFM Invoices " & strDate & "\"

    'Make the normal folder in DefPath
    MkDir FileNameFolder

    'Extract the files into the newly created folder
    Set oApp = CreateObject("Shell.Application")

    oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).Items


    MsgBox "You may find the unzipped files here: " & FileNameFolder
    TextBox1.Value = FileNameFolder

    On Error Resume Next
    Set FSO = CreateObject("scripting.filesystemobject")
    FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
End If
End Sub

标签: excelvba

解决方案


推荐阅读