Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
870 views
in Technique[技术] by (71.8m points)

excel - How to open a file from an archive in vba without unzipping the archive

I have a serie of archives : C:/archive1.zip, C:/archive2.zip, etc.

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

etc.

How can I read all the file Myfile.csv in vba ?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:emp directory or to any destination folder instead of C:emp:

entUnZip1File "C:archive1.zip", "C:emp", "folderlevel1/folderlevel2/folderlevel3/Myfile.csv"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...