我有一系列档案:C:/archive1.zip,C:/archive2.zip等。
我想从每个档案中只提取一个文件。 每个存档都具有相同的结构,文件可以在以下位置找到:
C:/archive1.zip/folderlevel1/folderlevel2/folderlevel3/Myfile.csv C:/archive2.zip/folderlevel1/folderlevel2/folderlevel3/Myfile.csv
等
如何在vba中读取所有文件Myfile.csv?
谢谢!
答案 0 :(得分:9)
你可以这样做:
'
' UnZip 1 file from a zip file:
'
Function entUnZip1File(ByVal strZipFilename, ByVal strDstDir, _
ByVal strFilename)
'
Const glngcCopyHereDisplayProgressBox = 256
'
Dim intOptions, objShell, objSource, objTarget
'
' Create the required Shell objects
Set objShell = CreateObject("Shell.Application")
'
' Create a reference to the files and folders in the ZIP file
Set objSource = _
objShell.NameSpace(strZipFilename).Items.item(CStr(strFilename))
'
' Create a reference to the target folder
Set objTarget = objShell.NameSpace(strDstDir)
'
intOptions = glngcCopyHereDisplayProgressBox
'
' UnZIP the files
objTarget.CopyHere objSource, intOptions
'
' Release the objects
Set objSource = Nothing
Set objTarget = Nothing
Set objShell = Nothing
'
entUnZip1File = 1
'
End Function
在宏中的任何位置,调用函数将文件解压缩到C:\ temp目录或任何目标文件夹而不是C:\ temp:
entUnZip1File "C:\archive1.zip", "C:\temp", "folderlevel1/folderlevel2/folderlevel3/Myfile.csv"