如何在不解压缩存档的情况下从 vba 中的存档中打开文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19716587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open a file from an archive in vba without unzipping the archive
提问by LouInNY
I have a serie of archives : C:/archive1.zip, C:/archive2.zip, etc.
我有一系列档案:C:/archive1.zip、C:/archive2.zip 等。
I want to extract only one file from each archive. Each archive has same structure and file can found under :
我只想从每个存档中提取一个文件。每个存档都具有相同的结构,文件可以在以下位置找到:
C:/archive1.zip/folderlevel1/folderlevel2/folderlevel3/Myfile.csv C:/archive2.zip/folderlevel1/folderlevel2/folderlevel3/Myfile.csv
C:/archive1.zip/folderlevel1/folderlevel2/folderlevel3/Myfile.csv C:/archive2.zip/folderlevel1/folderlevel2/folderlevel3/Myfile.csv
etc.
等等。
How can I read all the file Myfile.csv in vba ?
如何在 vba 中读取所有文件 Myfile.csv ?
Thanks!
谢谢!
回答by jacouh
You can do it as this:
你可以这样做:
'
' 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
And any where in your macro, call the function to extract the file into C:\temp directory or to any destination folder instead of C:\temp:
以及宏中的任何位置,调用函数将文件解压缩到 C:\temp 目录或任何目标文件夹而不是 C:\temp:
entUnZip1File "C:\archive1.zip", "C:\temp", "folderlevel1/folderlevel2/folderlevel3/Myfile.csv"