如何从vba中的存档打开文件,而不需要解压缩存档 [英] How to open a file from an archive in vba without unzipping the archive

查看:311
本文介绍了如何从vba中的存档打开文件,而不需要解压缩存档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列档案: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?



谢谢!

解决方案

您可以这样做:

 '
'从zip文件UnZip 1个文件:
'
函数entUnZip1File(ByVal strZipFilename,ByVal strDstDir,_
ByVal strFilename)
'
Const glngcCopyHereDisplayProgressBox = 256
'
Dim intOptions,objShell ,objSource,objTarget
'
'创建所需的Shell对象
设置objShell = CreateObject(Shell.Application)
'
'创建对文件的引用和ZIP文件中的文件夹
设置objSource = _
objShell.NameSpace(strZipFilename).Items.item(CStr(strFilename))
'
'创建对目标文件夹的引用
设置objTarget = objShell.NameSpace(strDstDir)
'
intOptions = glngcCopyHereDisplayProgressBox
'
'UnZIP文件
objTarget.CopyHere objSource,intOptions
'
'释放对象
设置objSource = Nothing
设置objTarget = Nothing
设置objShell = Nothing
'
entUnZip1File = 1
'
结束功能

调用函数将文件解压缩到C:\temp目录或任何目标文件夹而不是C:\temp:

  entUnZip1FileC:\archive1.zip,C:\temp,folderlevel1 / folderlevel2 / folderlevel3 / Myfile.csv
pre>

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!

解决方案

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:

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

这篇关于如何从vba中的存档打开文件,而不需要解压缩存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆