2

从理论上讲,我正在使用 Ron ( http://www.rondebruin.nl/win/s7/win002.htm ) 的代码将一堆 zip 文件解压缩到一个文件夹中。我相信我下面的代码是在我的“下载”目录中获取每个 zip 文件,使用不带“.zip”的 zip 文件的名称创建一个新文件夹,然后将文件解压缩到新文件夹中。我没有收到任何错误(很多时候人们收到运行时错误 91),但唯一发生的是它创建了一堆正确命名的文件夹,但它们都是空的。

Sub UnZipMe()

Dim str_FILENAME As String, str_DIRECTORY As String, str_DESTINATION As String

'Your directory where zip file is kept
str_DIRECTORY = "C:\Users\Jennifer\Downloads\"

'Loop through all zip files in a given directory
str_FILENAME = Dir(str_DIRECTORY & "*.zip")

Do While Len(str_FILENAME) > 0
    Call Unzip1(str_DIRECTORY & str_FILENAME)
    Debug.Print str_FILENAME
    str_FILENAME = Dir
Loop

End Sub

Sub Unzip1(str_FILENAME As String)
    Dim oApp As Object
    Dim Fname As Variant
    Dim FnameTrunc As Variant
    Dim FnameLength As Long

    Fname = str_FILENAME
    FnameLength = Len(Fname)
    FnameTrunc = Left(Fname, FnameLength - 4) & "\"

    If Fname = False Then
        'Do nothing
    Else
        'Make the new folder in root folder
        MkDir FnameTrunc

        'Extract the files into the newly created folder
        Set oApp = CreateObject("Shell.Application")
        oApp.Namespace(FnameTrunc).CopyHere oApp.Namespace(Fname).items
    End If
End Sub
4

1 回答 1

1

问题是您没有给 Windows 足够的时间来提取 zip 文件。在该行之后添加DoEvents,如下所示。

久经考验

    oApp.Namespace(FnameTrunc).CopyHere oApp.Namespace(Fname).items
    DoEvents
于 2013-11-06T13:05:52.573 回答