【问题标题】:How to open a file from an archive in vba without unzipping the archive如何在不解压缩存档的情况下从 vba 中的存档中打开文件
【发布时间】:2016-07-05 16:30:03
【问题描述】:

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

谢谢!

【问题讨论】:

标签: excel vba unzip


【解决方案1】:

你可以这样做:

'
' 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"

【讨论】:

  • 非常感谢雅库。它真的很好用。实际上,这比我想象的要容易。我想我还没有理解 Object.Namespace 函数的强大功能!
猜你喜欢
  • 1970-01-01
  • 2012-09-29
  • 2016-09-26
  • 2023-02-14
  • 1970-01-01
  • 1970-01-01
  • 2022-11-21
  • 2022-12-21
  • 2011-04-17
相关资源
最近更新 更多