我有一个存档文件,其中包含多个子文件夹。获取错误'运行时错误-2147024894(80070002)'...当提取压缩文件
例如:C:\Documents and Settings\Owner\Desktop\Macro\Intermediación Financiera\2013\12\BCO_Ind.zip
在BCO_Ind.zip包含此子文件夹scbm\2013\09\fileThatIWant.xls
这些子文件夹是每个存档文件不同,尽管它具有相同的名称。 事情是我想要最后一个子文件夹的最后一个文件。
我修改了代码从http://excelexperts.com/unzip-files-using-vba和www.rondebruin.nl/win/s7/win002.htm
问题是,我得到一个错误是: run-time error -2147024894(80070002)': Method 'Namespace' of Object 'IShellDispatch4' failed
。
我尝试从网站上搜索所有内容,但是我几乎没有找到解决方案将近一周。 下面是代码:
Sub TestRun()
'Change this as per your requirement
Call unzip("C:\Documents and Settings\Owner\Desktop\Macro\Intermediación Financiera\2013\12\", "C:\Documents and Settings\Owner\Desktop\Macro\Intermediación Financiera\2013\12\BCO_Ind.zip")
End Sub
Public Function unzip(targetpath As String, filename As Variant, Optional SCinZip As String, _
Optional excelfile As String) As String '(targetpath As String, filename As Variant)
Dim strScBOOKzip As String, strScBOOK As String: strScBOOK = targetpath
Dim targetpathzip As String, excelpath As String
Dim bzip As Boolean: bzip = False
Dim oApp As Object
Dim FileNameFolder As Variant
Dim fileNameInZip As Object
Dim objFSO As Scripting.FileSystemObject
Dim filenames As Variant: filenames = filename
If Right(targetpath, 1) <> Application.PathSeparator Then
targetpathzip = targetpath & Application.PathSeparator
Else
targetpathzip = targetpath
End If
FileNameFolder = targetpathzip
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oApp = CreateObject("Shell.Application")
''-----i get an error in here
For Each fileNameInZip In oApp.Namespace(filenames).Items
If objFSO.FolderExists(FileNameFolder & fileNameInZip) Then
objFSO.DeleteFolder FileNameFolder & fileNameInZip, True: Sleep 1000
End If
''-----i get an error in here too
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(filename).Items.item(CStr(fileNameInZip))
bzip = True
Next fileNameInZip
If bzip Then
excelpath = findexactfile(targetpath) ' this will go to the function that find the file from subfolders
Else
excelpath = ""
End If
searchfolder = FileNameFolder & fileNameInZip
finish:
unzip = excelpath
Set objFSO = Nothing
Set oApp = Nothing
End Function
我也勾出一些工具>开发宏引用,但它仍然得到同样的错误。我现在真的很紧张+沮丧。请帮我解决它。另外,是否有一个简单的代码作为我的参考文件在提取文件后从子文件夹中查找文件?我真的很感激,如果有人可以分享代码。
这可能是更容易的zip文件的所有内容复制到一个临时文件夹,并使用** ** objFSO方法来复制所需的文件。 “我想要最后一个子文件夹的最后一个文件”是什么意思?你的意思是你想要文件夹中没有子文件夹的文件? – PatricK
嗨帕特里克...我的意思是我想要的文件是在档案的最后一个子文件夹。归档文件(BCO_Ind.zip)包含这个子文件夹scbm \ 2013 \ 09 \ ** fileThatIWant.xls **因此,我想要这个文件** fileThatIWant.xls ** – user2851376
所以'fileThatIWant.xls'是唯一的文件归档?会不会有其他子文件夹,如'scbm \ 2013 \ 08 \ fileThatIWant.xls'?我能够调整你的代码,以在zip文件中显示文件名。你会用这个打开一次以上的zip文件吗(主文件夹中的所有zip文件)? – PatricK