通过Excel VBA代码解压缩文件

时间:2016-03-01 07:39:16

标签: excel vba excel-vba

团队,我正在努力从VBA代码中提取zip文件,但收到错误,这是我的代码

Sub Un_Zip_File()
Dim flname As String
Call PathCall
flname = Dir(impathn & "Transactions*.zip")
Call PathCall
Call UnZip_File(impathn, flname)
End Sub

Sub UnZip_File(strTargetPath As String, fname As Variant)
Dim oApp As Object, FSOobj As Object
Dim FileNameFolder As Variant

If Right(strTargetPath, 1) <> Application.PathSeparator Then
strTargetPath = strTargetPath & Application.PathSeparator
End If

FileNameFolder = strTargetPath

'destination folder if it does not exist
Set FSOobj = CreateObject("Scripting.FilesystemObject")
If FSOobj.FolderExists(FileNameFolder) = False Then
FSOobj.CreateFolder FileNameFolder
End If

Set oApp = CreateObject("Shell.Application")
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(fname).Items

Set oApp = Nothing
Set FSOobj = Nothing
Set FileNameFolder = Nothing

End Sub

当我运行Un_zip_file宏时,我在调试移动后收到错误“对象变量或未设置块变量”

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

5 个答案:

答案 0 :(得分:5)

这是另一个如何解压缩文件的示例。
宏将zip文件解压缩到固定文件夹"C:\test\"

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

    Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", _
                                        MultiSelect:=False)
    If Fname = False Then
        'Do nothing
    Else
        'Destination folder
        DefPath = "C:\test\"    ' Change to your path / variable
        If Right(DefPath, 1) <> "\" Then
            DefPath = DefPath & "\"
        End If

        FileNameFolder = DefPath

        '        'Delete all the files in the folder DefPath first if you want
        '        On Error Resume Next
        '        Kill DefPath & "*.*"
        '        On Error GoTo 0

        'Extract the files into the Destination folder
        Set oApp = CreateObject("Shell.Application")
        oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items

        MsgBox "You find the files here: " & FileNameFolder

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

答案 1 :(得分:1)

为避免出现消息错误:

1-每个“ \”更改“ /”

unzipToPath= Replace(unzipToPath, "/", "\\")

zippedFileFullName= Replace(zippedFileFullName, "/", "\\")

2-使用double((将参数如下:

ShellApp.Namespace((unzipToPath)).CopyHere
ShellApp.Namespace((zippedFileFullName)).Items

答案 2 :(得分:0)

我有完全相同的问题,但在MS Word中,尝试从.zip文件夹中提取文件。经过大量的实验和测试后,我发现后期绑定对象没有正确初始化,当我使用TypeName函数测试它们时,通常&#34;没有&#34;。

我在Windows 10和旧的Windows XP计算机上测试了我的代码,结果相同。我的所有测试都在Excel 2007和Excel 2016中进行。

将代码从后期绑定更改为早期绑定解决了该问题。

延迟绑定使用CreateObject函数初始化Shell.Application库中的对象。早期绑定需要为&#34; Microsoft Shell控件和自动化&#34;设置引用。您项目中的库。

要设置参考,请执行以下操作: 在VBA IDE中,使用“工具”菜单打开“引用”对话框。滚动可用参考列表,直到找到&#34; Microsoft Shell控件和自动化&#34;输入,然后单击复选框以选择该库,因此: The VBA References dialog, showing the "Microsoft Shell Controls and Automation" library after adding it to your project.

答案 3 :(得分:0)

在网络上的其他地方找到了,并认为这可能对您有帮助...

Sub UnzipAFile(zippedFileFullName As Variant, unzipToPath As Variant)

Dim ShellApp As Object

'Copy the files & folders from the zip into a folder
Set ShellApp = CreateObject("Shell.Application")
On Error Resume Next
ShellApp.Namespace(unzipToPath).CopyHere ShellApp.Namespace(zippedFileFullName).Items
On Error GoTo 0
End Sub

答案 4 :(得分:0)

我有同样的错误“对象变量或未设置块变量”。

通过添加对“ Microsoft Shell控件和自动化”-Shell32.dll的引用来解决。然后按此顺序定义和使用Shell对象。

不要跳过任何这些步骤。我还在此page中发布了完整的代码。

Dim wShApp As Shell

Set wShApp = CreateObject("Shell.Application")
Set objZipItems = wShApp.Namespace(zipFileName).items  

wShApp.Namespace(unZipFolderName).CopyHere objZipItems